My database structure:
products
-- name
-- price
-- qty
orders
-- user_id
-- status ... etc
order_product
-- order_id
-- product_id
-- qty
-- product_price_after_discount ( nullable )
I need to get most sold products with total_sells_of_product when:
- order
status == 'approved' - If there is a
product_price_after_discountignore the originall product price and count this.
MY wrong shut:
$topSellingProducts = Product::selectRaw('
products.id,
products.trade_name,
products.company_name
')
->leftJoin('order_product','order_product.product_id','=','products.id')
->where('status','approved')
->groupBy('products.id')
->orderByDesc('total_sales')
->get();