Using
$posts = Post::find($id)->comments()->get();
i can get children of single using
$posts = Post::first()->comments()->get();
I can get the first
using
$posts = Post::with('comments')->get();
i can't retrieve all children.
what am I doing wrong?
public function comments() {
return $this->hasMany('Comment','comment_id','id');
}
First argument of hasMany
should be full class name, like App\Comment
. Second argument should be the name of the field in comments
table that references the parent Post, like post_id
. If this field in your comments
table is already called post_id
, you can skip the second, and probably third, arguments, and just write:
return $this->hasMany('App\Comment');
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community