【问题标题】:Select element in where clause using join - Laravel使用连接选择 where 子句中的元素 - Laravel
【发布时间】:2020-09-24 07:07:47
【问题描述】:

我有一个查询,我在其中使用多个表上的联接获取数据。问题在于“vendor_products”表的连接。我想使用 where 子句从中获取数据,并且我从 select 元素中给出 vendor_id 的值。如果我硬编码数据即将到来的值,但如果我使用“orders_vendor_id”,则查询不起作用。

Order::select(
        'orders.vendor_id as vendor_id',
        'order_items.product_id as product_id',
        'vendor_products.stock_id as stock_id',
        'vendors.stock as stock',
    )
    ->join('order_items',function($join){
        $join->on('order_items.orders_id', '=','orders.id')
        ->whereNotNull('final_delivery_price');
    })
    ->join('vendor_products',function($join){
        $join->on('vendor_products.product_id', '=','order_items.product_id')
        ->where('vendor_products.vendor_id', 'orders.vendor_id');
    })
    ->join('vendors',function($join){
        $join->on('vendors.id', '=','orders.vendor_id');
    })
    ->where('orders.status', 'delivered')
    ->where('orders.created_at','>=', Carbon::now()->subDays(14))
    ->groupBy('orders.vendor_id','order_items.product_id','vendor_products.vendor_stock_id','vendors.vendor_stock')
    ->orderBy('order_items.product_id');

【问题讨论】:

    标签: php laravel laravel-5 laravel-query-builder


    【解决方案1】:

    我通过在 where 子句中使用 DB::raw 解决了我的问题

    Order::select(
                'orders.vendor_id as vendor_id',
                'order_items.product_id as product_id',
                'vendor_products.stock_id as stock_id',
                'vendors.stock as stock',
            )
            ->join('order_items',function($join){
                $join->on('order_items.orders_id', '=','orders.id')
                ->whereNotNull('final_delivery_price');
            })
            ->join('vendor_products',function($join){
                $join->on('vendor_products.product_id', '=','order_items.product_id')
                ->where('vendor_products.vendor_id', DB::raw('orders.vendor_id'));
            })
            ->join('vendors',function($join){
                $join->on('vendors.id', '=','orders.vendor_id');
            })
            ->where('orders.status', 'delivered')
            ->where('orders.created_at','>=', Carbon::now()->subDays(14))
            ->groupBy('orders.vendor_id','order_items.product_id','vendor_products.vendor_stock_id','vendors.vendor_stock')
            ->orderBy('order_items.product_id');
    

    【讨论】:

      【解决方案2】:

      查询生成器中的'where'方法将第三个参数作为值而不是列(第二个参数默认为'=') 如果您想按列值进行比较...请改用 whereColumn:

       ->join('vendor_products',function($join){
              $join->on('vendor_products.product_id', '=','order_items.product_id')
              ->whereColumn('vendor_products.vendor_id', 'orders.vendor_id');
          })
      

      更多关于 whereColumn in:

      https://laravel.com/docs/7.x/queries#where-clauses

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-21
        • 2015-10-26
        • 1970-01-01
        相关资源
        最近更新 更多