samedi 26 octobre 2019

Query: product quantity with customizable variable like color, size

I want to make a report from stock_reports table where I want to show quantity of every product with variable like color, size based on created_at means every lot. i have used following Query but i needs some improvement.

$stockReport = DB::table('products')
        ->rightjoin('stock_reports','stock_reports.product_id','products.id')
         ->join('sizes','sizes.id','stock_reports.size_id')
         ->join('colors','colors.id','stock_reports.color_id')
        ->select('products.product_name','stock_reports.created_at'
            DB::raw('sum(quantity)'))

        ->groupby('stock_reports.created_at','products.product_name')
        ->orderby('stock_reports.created_at','desc')
        ->get();

Output should Like:

Name           Quantity    CREATED_AT
product_name1  XL-BLUE-15, L-RED-20 2019-08-12 
product_name2  L-BLUE-15, S-RED-20 2019-08-12
product_name1  M-BLUE-15, L-RED-20 2019-08-13

My table Structure:

products:

 id product_name

stock_reports

 id
 product_id
 size_id
 quantity
 color_id
 sold_price

sizes

  id
  size_name

colors

  id
  color_name


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2pkOSob
via IFTTT

Aucun commentaire:

Enregistrer un commentaire