求解决 Laravel with () limit 的问题 附带解决方案
比如如何取得每篇文章中的前10条评论
下面为错误写法:
essay::with(['comment' => function($query) {
$query->take(10)
}])
谢谢各位大佬的帮助!
但是最终效果都不理想。
https://github.com/laravel/framework/issue...
在上面的issues找到解决方法
贴一下自己的代码
//这里我是反向关联,正向关联也可以的。
public function products()
{
return $this->belongsToMany(Hproduct::class,'hproduct_has_type','type_id','product_id');
}
public function topTenproducts()
{
return $this->products()->with('supplier')->where('is_sale','=',1)->limit(4);
}
public function getProdcutsWithtopTenproducts($col)
{
$model = $this->whereIn('id',$col)->get();
$model->each(function ($model) {
$model->load('topTenproducts');
});
return $model->toArray();
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: