How to load Laravel with nested models and limit the nested models?

后端 未结 1 1373
我在风中等你
我在风中等你 2021-01-17 05:06

Hello guys I am using Laravel 5.6 I have three tables in the database posts,comments and replies and three models Post , Comment , Reply

the relations are as follow

1条回答
  •  囚心锁ツ
    2021-01-17 05:37

    There is no native support for this in Laravel.

    I created a package for it: https://github.com/staudenmeir/eloquent-eager-limit

    Use the HasEagerLimit trait in both the parent and the related model.

    class Post extends Model {
        use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
    }
    
    class Comment extends Model {
        use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;
    }
    

    Then you can apply ->take(3) to your relationship.

    The same goes for the replies.

    0 讨论(0)
提交回复
热议问题