【问题标题】:How to fetch the un-ordered product from the product table in laravel with pagination?如何通过分页从 laravel 中的产品表中获取未订购的产品?
【发布时间】:2017-10-18 14:31:35
【问题描述】:

我有两张桌子 产品表:列(id、product_name、product_image、price) 订单表:列(id,Product_id,user_id,ordered_on)

我有一个列表页面(最初我列出了产品表中的所有产品) 如果产品已添加到购物车,则必须根据用户将其从购物车中删除。

我已经这样写了查询:

 $menuspecial=Product::paginate(10)->toArray();
 foreach($menuspecial as $menu){
        $menuinfo = Cart::where('product_id',$menu['id'])->where('user_id',$id)->first();
        if(!isset($menuinfo)){
            $menuvalue[] = $menu->get();
        }

在 api 响应期间,我没有得到分页数据(例如下一个 url,page..ect)

提前致谢。

【问题讨论】:

标签: laravel laravel-eloquent laravel-query-builder


【解决方案1】:

您想删除购物车项目,使其不再出现在产品菜单中。这样做。

    $cartProducts = Cart::where('user_id',$id)
        ->get()
        ->pluck('product_id')
        ->toArray();

    $productMenu = Product::whereNotIn('id', $cartProducts)
        ->paginate(10);

【讨论】:

  • 下一个页面的 url 是什么,如何处理。
  • 您可以在官方文档laravel.com/docs/5.4/pagination 中阅读更多关于分页的信息,它使用默认设置为“page”的页面获取输入生成下一页 url,并且根据剩余的结果集。
猜你喜欢
  • 1970-01-01
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-03
  • 2022-06-16
相关资源
最近更新 更多