these are my relations..
User model
public function posts()
{
return $this->hasMany('Post');
}
Post model
public function user()
{
return $this->belongsTo('User');
}
public function replies()
{
return $this->hasMany('Reply');
}
Reply Model
public function post()
{
return $this->belongsTo('Post');
}
And schema is
users: id name
post:id user_id
comments:id post_id
I want to display the posts that has comments using
$user = User::find(Auth::user()->id);
$templatedata = $user->posts()->replies->get()
but its giving call to undefined method.. Please help me..!!
Try this:
$templatedata = $user->posts()->with('replies')->get();
The $templatedata
will be a Collection of Post
objects each having a property ->replies
which will contain a Collection of Reply
objects.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community