Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用嵌套模型加载Laravel并限制嵌套模型?_Laravel_Eloquent_Query Builder_Laravel Query Builder - Fatal编程技术网

如何使用嵌套模型加载Laravel并限制嵌套模型?

如何使用嵌套模型加载Laravel并限制嵌套模型?,laravel,eloquent,query-builder,laravel-query-builder,Laravel,Eloquent,Query Builder,Laravel Query Builder,大家好,我使用的是Laravel5.6,我在数据库中有三个表Post,comments和Reply,还有三个模型Post,Comment,Reply 关系如下:一篇帖子有很多评论,一篇评论有很多回复。我创建了一个路由,当点击时将返回一些数据;但是,我希望这些数据以一种特定的方式读取此示例: 假设我在我的数据库中有6篇文章,每篇文章有6条评论,每条评论也有6条回复,我只想返回前3篇文章以及每篇文章的前3条评论,还有每条评论的前3条回复 //this is a function inside a c

大家好,我使用的是Laravel5.6,我在数据库中有三个表Post,comments和Reply,还有三个模型Post,Comment,Reply

关系如下:一篇帖子有很多评论,一篇评论有很多回复。我创建了一个路由,当点击时将返回一些数据;但是,我希望这些数据以一种特定的方式读取此示例:

假设我在我的数据库中有6篇文章,每篇文章有6条评论,每条评论也有6条回复,我只想返回前3篇文章以及每篇文章的前3条评论,还有每条评论的前3条回复

//this is a function inside a controller 
//and for sure I have make sure to make use of the models namespaces 

public function test(){
    $posts = Post::with(['comments' => function($data){
        return $data->take(3);
    },
    'comments.replies' => function($data){
        return $data->take(3);
    }])->paginate(3);

    //returning the posts
    return $posts
}
这种方法是有效的,它返回前3篇文章,它只返回前3条评论和前3条回复,但对于其他文章,我只得到一个空的评论键,因此没有回复

希望你明白我的问题,请帮忙 对不起,有个大问题
提前感谢。

在Laravel没有本地支持

我为它创建了一个包:

在父模型和相关模型中都使用
haskeetLimit
特征

类后扩展模型{
使用\staudenMir\elounceTeagerLimit\hasWangerLimit;
}
类注释扩展模型{
使用\staudenMir\elounceTeagerLimit\hasWangerLimit;
}
然后,您可以对您的关系应用
->take(3)

回复也是如此。

可能重复