【发布时间】:2019-07-09 03:54:40
【问题描述】:
我有包含自定义产品的集合,我需要对这些集合产品进行分页,但我收到错误。
数据
- 通过此查询,我可以获得我的收藏及其产品,但我无法对产品进行分页。
$collection = Collection::where('slug', $slug)->where('status', 'active')->with('products')->first();
- 使用此查询我收到错误
$collection = Product::with('collections')->whereHas('collections', 函数($query) 使用($slug) { $query->where('slug', $slug)->where('status', 'active')->first(); });
错误
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'shopping.product_id' doesn't exist (SQL: select * from `collection_products` inner join `product_id` on `collection_products`.`id` = `product_id`.`collection_product_id` where `products`.`id` = `product_id`.`product_id` and `slug` = test and `status` = active limit 1)
代码
Product model
public function collections()
{
return $this->belongsToMany(CollectionProduct::class, 'product_id');
}
Collection model
public function collection(){
return $this->belongsToMany(CollectionProduct::class);
}
public function products(){
return $this->belongsToMany(Product::class, 'collection_products', 'collection_id', 'product_id');
}
CollectionProduct model
public function collection()
{
return $this->belongsTo(Collection::class, 'collection_id','id');
}
Controller默认查询
public function single($slug){
$collection = Collection::where('slug', $slug)->where('status', 'active')->with('products')->first();
return view('front.collections.single', compact('collection'));
}
问题
- 如何获得具有分页功能的收藏品?
【问题讨论】:
标签: php laravel pivot-table