Laravel 5.1:使用 hasManyThrough() 对模型进行分页
Laravel 5.1: Pagination on Models with hasManyThrough()
我有 "Brand" hasManyThrough
"MarketProduct" 到 "Product"。所以,
Brand >> Product >> MarketProduct
其中 >>
表示 hasMany
关系。我可以使用 Brand
.
中定义的 hasManyThrough()
方法获取 MarketProducts
的集合 (Illuminate\Database\Eloquent\Collection
),如下所示
$collection = Brand::find($someId)->marketProducts;
问题是 $colletion
不是查询生成器或 Eloquent 的实例,所以我不能使用 ->paginate($num)
。使用 hasManyThrough
时是否可以使用默认分页功能,还是我需要手动构建查询才能使用分页?
如果您查看 document,它会提到
...since all relationships also serve as query builders, you can add
further constraints to which comments are retrieved by calling the
comments method and continuing to chain conditions onto the query...
所以解决方案是:
$collection = Brand::find($someId)->marketProducts();
我有 "Brand" hasManyThrough
"MarketProduct" 到 "Product"。所以,
Brand >> Product >> MarketProduct
其中 >>
表示 hasMany
关系。我可以使用 Brand
.
hasManyThrough()
方法获取 MarketProducts
的集合 (Illuminate\Database\Eloquent\Collection
),如下所示
$collection = Brand::find($someId)->marketProducts;
问题是 $colletion
不是查询生成器或 Eloquent 的实例,所以我不能使用 ->paginate($num)
。使用 hasManyThrough
时是否可以使用默认分页功能,还是我需要手动构建查询才能使用分页?
如果您查看 document,它会提到
...since all relationships also serve as query builders, you can add further constraints to which comments are retrieved by calling the comments method and continuing to chain conditions onto the query...
所以解决方案是:
$collection = Brand::find($someId)->marketProducts();