Laravel Layered Relationships

Is it possible to make a request for a multi-level request with Eloquent at a deeper level than level 1? My tables look like this:

post_comments-> id | comment | post_id | user_id | ...

post_comment_replies-> id | reply | post_comment_id | user_id | ...

users-> id | name | ....

user_data-> id | avatar | ...

And so I want to ask if it is possible to get Comments for Post with all the answers and user data for the person who answered the comment in 1 request with Eloquent.

This is what my model looks like Comments :

class PostComment extends Model{
public function replies(){
    return $this->hasMany(PostCommentAwnsers::class);
}

public function user() {
    return $this->belongsTo(User::class);
}
public function userInfo(){
    return $this->belongsTo(UserInfo::class,'user_id','user_id');
}}

public function index($id){
    $posts = Post::find($id);
    $postComments = PostComment::where('post_id','=',$id)->paginate(5);

    return view('post.show',[
        'post' => $post,
        'postComments' =>$postComments
    ]);
}

, , . , - , .

+4
1

: https://laravel.com/docs/5.3/eloquent-relationships#eager-loading

:

$posts = Post::with('comment')->get();

, :

$posts = Post::with('comment.reply')->get();
+7

Source: https://habr.com/ru/post/1664403/


All Articles