【问题标题】:Laravel 分页数据透视表
【发布时间】:2019-07-09 03:54:40
【问题描述】:

我有包含自定义产品的集合,我需要对这些集合产品进行分页,但我收到错误。

数据

  1. 通过此查询,我可以获得我的收藏及其产品,但我无法对产品进行分页。

$collection = Collection::where('slug', $slug)->where('status', 'active')->with('products')->first();

  1. 使用此查询我收到错误

$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'));
}

问题

  1. 如何获得具有分页功能的收藏品?

【问题讨论】:

    标签: php laravel pivot-table


    【解决方案1】:

    几件事:

    您正在尝试在此行的关系查询中调用 first() 方法:

    $collection = Product::with('collections')->whereHas('collections', function($query) use($slug) { $query->where('slug', $slug)->where ('status', 'active')->first(); });

    first()get() 方法用于执行查询,因此您应该将它们放在 eloquent 方法链的末尾:

    $collection = Product::with('collections')
                         ->whereHas('collections', function($query) use($slug) { 
                             $query->where('slug', $slug)->where('status', 'active'); 
                         })
                         ->get();
    

    https://laravel.com/docs/5.7/eloquent#retrieving-models

    但是,如果你想对产品列表进行分页,那么你真正想要的是paginate() 方法:

    $collection = Product::with('collections')
                         ->whereHas('collections', function($query) use($slug) { 
                             $query->where('slug', $slug)->where('status', 'active'); 
                         })
                         ->paginate(20);   // will return 20 products per page
    

    https://laravel.com/docs/5.7/pagination

    此外,Product 模型上的 collections() 方法将 product_id 列为连接表,并且正在连接到 CollectionProduct 模型而不是 Collection 模型。

    为您的 Product 模型尝试此方法:

    public function collections()
    {
        return $this->belongsToMany(Collection::class, 'collection_products', 'product_id', 'collection_id');
    }
    

    【讨论】:

    • 它在存在的地方返回SQLSTATE[42S02]: Base table or view not found: 1146 Table 'shopping.product_id' doesn't exist (SQL: select count(*) as aggregate from products`(从collection_products内部连接product_idcollection_productsid=product_id.collection_product_idproductsid = product_id.product_id and slug = test and status = active))`
    • @mafortis 你能发布每个模型的表定义吗?将它们添加到您上面的问题中。
    • @mafortis nvm,我想我找到了问题所在。请参阅我的更新答案。
    • 谢谢,我删除了我的解决方案,将接受你的解决方案
    【解决方案2】:

    我使用以下代码并且工作得很好

    第一步获取产品

    $product = Product()->find($product_id); 
    

    比我得到集合和分页

    $collections = Product::find($product_id)->collections->paginate(20); 
    

    例如,我在site 中使用了上面的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 2017-12-15
      • 2021-08-17
      • 1970-01-01
      • 2015-09-10
      相关资源
      最近更新 更多