@snapey can u suggest me.
dont think that iam often tagging uu. sorry
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Guys iam working with a project hotel management system.
i have three tables
Table 1: OpenStock with columns
id
open_date
product_id
opening_qty
// its respective model
public function product()
{
return $this->belongsTo('App\Product', 'product_id');
}
Table 2 : Purchase with columns
id
purchase_date
product_id
product_qty
//its respective model
public function product()
{
return $this->belongsTo('App\Product', 'product_id');
}
Table 3 :StockIssue with columns
id
product_id
issued_date
issued_qty
//its respective model
public function product()
{
return $this->belongsTo('App\Product', 'product_id');
}
My products table has columns
id
product_name
what i expect is need to fetch records from OpenStock table, Purchase table, and StockIssue table and add the quantity of same product_id (sum the OpenStock and Purchase) and subtract it from stockIssue table
this what the output i expect
product openstock purchasestock totalstock issuedstock balance
potato 10 20 30 2 28
tomato 12 20 32 30 2
iam expecting the output like above.kindly some one give idea please..
i was so confused that from where i should start the query. from product table or purchase table or openstock table or issued table.
i know that need to used joins. but whenever joins are involved iam so confused.
so kindly some one suggest ideas please
@snapey @siangboon and sorry for delay
Thank you. thank you soo much. it worked for me.
what i did is
$prd = Product::
with([
'openstock' => function ($query) use ($request) {
$query->where('hotel_id', Auth::user()->hotel_id)->whereBetween('open_date', [$request->from_date, $request->to_date])->
selectRaw('product_id,SUM(open_qty) as totalopenqty')
->groupBy('product_id');
},
])->
with([
'specificpurchasebill' => function ($query) use ($request) {
$query->where('hotel_id', Auth::user()->hotel_id)->whereBetween('d_date', [$request->from_date, $request->to_date])->
selectRaw('product_id,SUM(qty) as totalpurchaseqty')
->groupBy('product_id');
},
])->
with([
'storeissue' => function ($query) use ($request) {
$query->where('hotel_id', Auth::user()->hotel_id)->whereBetween('idate', [$request->from_date, $request->to_date])->
selectRaw('product_id,SUM(issued_qty) as totalissuedqty')
->groupBy('product_id');
},
])->get();
ans then passed it blade file. with foreach i got what i expected. thank you guys. @snapey you made me think my self. thank you..
Kindly if possible please suggest idea for the below threads
Please or to participate in this conversation.