How can I get video of specific model? - laravel

I want to display videos for each model in my table, where to define they are connected by their ids?
In my controller:
public function index()
{
$model_video = NewModelVideos::all();
return view('admin.model_new_videos.index')
->with('models', $models)
->with('model_video', $model_video);
}
in my Model:
class NewModelVideos extends Model
{
public function model()
{
return $this->belongsTo('App\Models\NewModels\NewModel', 'model_id');
}
}
class NewModel extends Model
{
public function videos()
{
return $this->hasMany('App\Models\NewModels\NewModelVideos', 'model_id', 'id');
}
}
and View:
#foreach($model_video as $model)
{{ $model->video }}
#endforeach

You should update your code like this:
public function index()
{
$model_video = NewModelVideos::all();
$models = NewModel::all();
return view('admin.model_new_videos.index')
->with('models', $models)
->with('model_video', $model_video);
}
and blade:
#foreach($models as $model)
{{ $model->videos }} //get videos
#endforeach
#foreach($model_video as $video)
{{ $video->model }} //get model
#endforeach

Related

Cannot call to model of polymorphic relation

I'm trying to reach a (ex.) visitable_id: 38 visitable_type: App\Thread in a polymorphic relationship. I can do it with the User with the code below, but can't seem to do it with the thread:
Blade:
#foreach ($visits as $visit)
#if($visit->visitable_type == 'App\User')
Viewing member profile {{ $visit->user->name }}
#endif
#if($visit->visitable_type == 'App\Thread')
Viewing thread <a href="/forums/{{ $visit->channel->slug }}/{{ $visit->slug }}/">
{{ $visit->title }}</a>
#endif
#endforeach
Controller:
$visits = Visit::where('user_id', $user->id)->get();
Models:
User:
public function visits()
{
return $this->morphMany(Visit::class, 'visitable');
}
public function visit()
{
return $this->morphOne(Visit::class, 'visitable');
}
Models: Visit:
public function user()
{
return $this->belongsTo(User::class);
}
public function thread()
{
return $this->belongsTo(Thread::class);
}
public function visitable()
{
return $this->morphTo();
}
Models: Thread:
public function visits()
{
return $this->morphMany(Visit::class, 'visitable');
}
public function visit()
{
return $this->morphOne(Visit::class, 'visitable');
}
Yeah I truly don't understand what is the issue here because I can get the visitable_id and visitable_type when it is the User, but not when it is a thread. Can anyone please help me?
Thank you!!
I have to call the relationship in the case of a thread (so $visit->visitable->channel->slug)

how to display the username of the comment in the post with relation table

my relational database image
{{ \App\User::where('id', $comment->user_id)->value('name') }}
if I use the code above
name user show in all post
my model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Komen extends Model
{
protected $table='komentar_post';
}
my controller
public function index()
{
$post=Post::all();
$comment=Komen::all();
$user=User::all();
// dd($id);
return view('home',compact('post','comment','user'));
}
my view
#foreach($comments as $cm)
{{ \App\User::where('id', $cm->user_id)->value('name') }}
#endforeach
what the corect query i must use
You need to create relationship in your Komen model. It's something like this:
class Komen extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
Then, you can Eager Load that relationship when fetching the Komen.
PostsController.php
public function show(Post $post)
{
$comments = $post->comments()->with('user')->paginate();
return view('posts.show', compact('comments'));
}
Then, you can access user data when displaying each comments:
posts/show.blade.php
#foreach($comments as $comment)
{{ $comment->user->name }}
#endforeach
{!! $comments->links() !!}
Update
Ok, if you need to displaying comments when you're displaying all posts, first you need to setup the relationship!
Post.php
class Post extends Model
{
public function comments()
{
return $this->hasMany(Komen::class, 'post_id');
}
}
Komen.php
class Komen extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
PostsController.php
public function index()
{
$posts = Post::with('comments.user')->paginate();
return view('home', compact('posts'));
}
home.blade.php
#foreach($posts as $post)
#foreach($post->comments as $comment)
{{ $comment->user->name }}
#endforeach
#endforeach
{!! $posts->links() !!}

Counting total posts by a user in the blade view

I have sent a collection of all posts in my blog to my index view and then used the following code to count the total posts made by each user.
<p class="joined-text">Posts: {{count(App\Posts::where('user_id', $post->user->id)->get())}}</p>
Is this bad practice to do this from within the blade view? If it is how would I achieve this?
Models
class Posts extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function comments()
{
return $this->hasMany(Comments::class, 'post_id');
}
}
class User extends Authenticatable
{
public function posts()
{
return $this->hasMany('\App\Posts::class');
}
public function comments()
{
return $this->hasMany(Comments::class);
}
}
Simple solution:
<p class="joined-text">Posts: {{ App\Posts::where('user_id', $post->user_id)->count() }}</p>
Updated
Complete and better solution:
Post.php:
public function user(){
return $this->belongsTo(App\User::class);
}
User.php:
public function posts(){
return $this->hasMany(App\Post::class);
}
public function getPostsCountAttribute(){
return $this->posts()->count();
}
blade:
<p class="joined-text">Posts: {{ $post->user->posts_count }}</p>
Yes it is bad.
You could use relationships if have the user_id field in posts table
class User extends Model
{
public function posts()
{
return $this->hasMany('App\Post');
}
}
In controller
return view('sth')->with(['posts'=>$user->posts]);
Then in view
$posts->count();
Or just getting counts if you don't need posts
$postCount = $user->posts()->count();

Laravel, Using two user relationships in model

Connection Model :
protected $table = 'connections';
protected $fillable = ['user_id_1','user_id_2','connection_status'];
public function user_id_1()
{
return $this->belongsTo('App\User', 'user_id_1');
}
public function user_id_2()
{
return $this->belongsTo('App\User', 'user_id_2');
}
Controller :
public function show($id){
$user_id_1_connections = Connection::whereUser_id_1AndConnection_status($id, 1)->get();
$user_id_2_connections = Connection::whereUser_id_2AndConnection_status($id, 1)->get();
return view('connection.showConnection',['user_id_1_connections' => $user_id_1_connections, 'user_id_2_connections' => $user_id_2_connections]);
}
Blade:
#foreach($user_id_1_connections as $user_id_1_connection)
{{ $user_id_1_connection->user_id_1 ? $user_id_1_connection->user_id_1->name : 'unknown' }}
#endforeach
#foreach($user_id_2_connections as $user_id_2_connection)
{{ $user_id_2_connection->user_id_2 ? $user_id_2_connection->user_id_2->name : 'unknown' }}
#endforeach
I am making two relationships to user in my model Connection.php. I'm getting the error: "Trying to get property of non-object " in line 2nd and 5th in blade.
First change:
public function user_id_1()
{
return $this->belongsTo('App\User', 'user_id_1');
}
public function user_id_2()
{
return $this->belongsTo('App\User', 'user_id_2');
}
into:
public function user1()
{
return $this->belongsTo('App\User', 'user_id_1');
}
public function user2()
{
return $this->belongsTo('App\User', 'user_id_2');
}
to avoid collisions between table columns and relationships
Instead of this:
public function show($id){
$user_id_1_connections = Connection::whereUser_id_1AndConnection_status($id, 1)->get();
$user_id_2_connections = Connection::whereUser_id_2AndConnection_status($id, 1)->get();
return view('connection.showConnection',['user_id_1_connections' => $user_id_1_connections, 'user_id_2_connections' => $user_id_2_connections]);
}
you can do:
public function show($id){
return view('connection.showConnection', ['connection' => Connection::findOrFail($id)]);
}
And now your template could look like this:
User 1 name: {{ $connection->user1 ? $connection->user1->name : 'unknown' }}
User 2 name: {{ $connection->user2 ? $connection->user2->name : 'unknown' }}
But to be honest it's hard to get what you really want to achieve - you are using strange function whereUser_id_1AndConnection_status what you should rather don't do so probably you should read carefully relationships documentation for Laravel.

posts and attachments, Undefined property: Illuminate\Database\Eloquent\Collection::$name

when i try to get the attachment's name like this:
{{$post->attachments->name}}
i get the following error
Undefined property: Illuminate\Database\Eloquent\Collection::$name (View: C:\wamp\www\Sites\app\views\public\categories\show.blade.php)
Post model
class Post extends \Eloquent implements SluggableInterface {
public function category(){
return $this->belongsTo('Category');
}
public function attachments(){
return $this->hasMany('Attachment');
}
}
Attachment Model
class Attachment extends \Eloquent {
protected $fillable = ['name', 'type', 'extension', 'user_id', 'post_id', 'size'];
public function post(){
return $this->belongsTo('Post');
}
}
CategoriesController
class CategoriesController extends \BaseController {
public function show($id, $slug = null)
{
$category = Category::find($id);
$posts = Post::whereCategoryId($category->id)->orderBy('date', 'DESC')->paginate(4);
return View::make('public.categories.show', compact('category', 'posts'));
}
}
categorie View
#foreach($posts as $post)
{{$post->title}} // work fine
{{$post->body}} // work fine
{{$post->attachments}} // return this :
[
{
"id":14,
"name":"29-01-2015-134",
"type":"image\/jpeg",
"extension":"jpg",
"created_at":"2015-01-29 13:04:35",
"updated_at":"2015-01-29 13:04:35",
"user_id":1,
"post_id":134,
"size":136130
}
]
#endforeach
any ideas?!!!
According to your relationship definition there can be many attachments, that means the relation will return a collection of models. You can either get only the first one:
#if($attachment = $post->attachments()->first())
{{ $attachment->name }}
#endif
Or loop over all attachments
#foreach($post->attachments as $attachment)
{{ $attachment->name }}
#endforeach

Resources