i followed the convention to avail the required data with compact method to view Schema User Table
id,name,email,password
Schema post Table
id,user_id,title,body,photo
Schema comment Table
id,post_id,user_id,comments,photo
in user model
public function post()
{
return $this->hasMany(post::class);
}
public function comments()
{
return $this->hasMany(comment::class);
}
in post model
Public function User()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->hasMany(comment::class);
}
in comment model
public function posts()
{
return $this->belongsTo('App\post');
}
in controller
$list = User::with('post','comments')->paginate(1);
return view('dynamic',compact('list'));
in view
@foreach($lists->comments as $comments)
{{$comments->name}}
@endforeach
post showing successfully according to users
and comments showing successfully according to posts
but commented name not showing up // please help
how to make relationship for fetch comments with commented user name
please help thanks in advance
$list = User::with('post.comments')->paginate(1);
belongsTo
relation and plural function name forhasMany