Here I would add $product_id condition in addselect query on whereRaw but its show error both error screenshots added.
$product_id condition is necessary for desire output. Is there any alt way to pass product_id condition on whereRaw
I don't know what happens here
public function viewOrder($product_id)
{
// Get all product with variant with color with category with stock
// DB::enableQueryLog();
$data=DB::table('product_masters')
->join('product_varients','product_varients.product_id','=','product_masters.id')
->join('color_masters','color_masters.id','=','product_varients.color_id')
->join('product_category_masters','product_category_masters.id','=','product_masters.category_id')
//->groupBy('product_masters.id')
->orderBy('product_masters.id')
->where('product_masters.id',$product_id)
->select('product_varients.id as product_varients_id',
'product_masters.name as product_name',
'product_masters.id as product_id',
'product_masters.unit_id as product_unit',
'color_masters.name as color_name',
'color_masters.id as color_id',
'color_masters.color_hexcode',
'color_masters.pattern_img',
'product_category_masters.name as product_category_name',
'product_category_masters.id as product_category_id',
)
->addSelect(array('stock_quantity' => function($query)
{
$query->select(DB::raw('COUNT(`product_varients_stocks`.`id`) as `total_qty`'))
->from('product_varients_stocks')
->whereRaw('product_varients_stocks.varient_id = product_varients.id')
->limit(1);
},'next_quantity' => function($query1)
{
// $purchase_items=DB::table('purchases')
$query1->select(DB::raw('SUM(`purchase_items`.`qty`) as `qty_sum`'))
->from('purchases')
->join('purchase_items','purchases.id','=','purchase_items.purchase_id')
->whereRaw('purchases.po_status','1')
->whereRaw('purchase_items.product_id ',$product_id) // Show error here
->whereRaw('purchase_items.product_id = product_masters.id')
->whereRaw('purchase_items.varient_id = product_varients.id')
->limit(1);
}))
->get();
// $query = DB::getQueryLog();
// // echo "<pre>";
// info($query);
return $data;
}
Error screenshot