[英]Different db connection for models using hasManyThrough relationship in laravel 5.1
[英]Laravel 5.1: Pagination on Models with hasManyThrough()
我有“品牌”,有hasManyThrough
通过“产品”通过“市场产品”。 所以,
Brand >> Product >> MarketProduct
其中>>
代表hasMany
关系。 我可以使用Brand
定义的hasManyThrough()
方法来获取hasManyThrough()
的集合( Illuminate\\Database\\Eloquent\\Collection
MarketProducts
Illuminate\\Database\\Eloquent\\Collection
),如下所示。
$collection = Brand::find($someId)->marketProducts;
问题是$colletion
不是查询生成器或Eloquent的实例,所以我不能使用->paginate($num)
。 使用hasManyThrough
时是否可以使用默认的分页功能,或者我是否需要手动构建查询才能使用分页?
如果您看一下文档 ,它会提到
...由于所有关系也都可以用作查询生成器,因此您可以通过调用comments方法并继续将条件链接到查询上,从而为检索注释添加更多约束...
因此解决方案是:
$collection = Brand::find($someId)->marketProducts();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.