Laravel查询,最畅销的产品

时间:2019-04-29 10:00:48

标签: laravel eloquent

我想在laravel中找到今天销量最大的产品:

order:id,id_client
order_items:id_order,id_product,数量,价格

 $max=Order::join('orders_item','orders.id', '=','orders_item.id_order')

            ->whereDate('orders.created_at','=', date('Y-m-d'))
            ->select('id_product')
            ->groupBy('id_order')
            ->max('quantity');

$most_sold_product_today=Order::join('orders_item','orders.id', '=','orders_item.id_order')

          ->where('orders_item.quantity','=',$max)
            ->whereDate('orders.created_at','=', date('Y-m-d'))
            ->get();

但是我没有得到5种大多数产品,但是我知道查询不正确。你能帮我么 ?

1 个答案:

答案 0 :(得分:0)

  $max=DB::table('orders_item')
        ->join('orders','orders.id', '=','orders_item.id_order')
        ->select('orders_item.id_product')
        ->whereDate('orders.created_at','=', date('Y-m-d'))
        ->groupBy('orders_item.id_order')
        ->max('orders_item.quantity');

 $most_sold_product_today=Order::
        join('orders_item','orders.id', '=','orders_item.id_order')
        ->where('orders_item.quantity','=',$max)
        ->whereDate('orders.created_at','=', date('Y-m-d'))
        ->get();