Have you tried another order?
A::where('id', 1)->with('b')->with('c')->first();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello laravel dev.
I have three tables let's say A, B, C
A
-----------
-id
-main_location_id // **main location entry = 1**
B
-----------
-id
-a_id
-allocated_qty
C
-----------
-id
-a_id
-location_id // **main location enrty + other locations entry- 1,2,3**
-total_qty
-aval_qty
Relation:
table A has many of B
table A also has many of C
Query:
A::with(['B'])
->with(['C']) // here I got all location object from C tables, but I only want main_location_id = location_id data object + its qty
->where('a.id', 1)
->first();
I don't understand how will I get location_id, total_qty, aval_qty from table C through table A which has c.location_id = a.main_location_id
Even I don't know if I'm doing right?
is this possible?
please help me with this
Many thanks!
@M@rty No you should add
use \Awobaz\Compoships\Compoships;
to your Model.
In your case relation:
public function c()
{
return $this->hasMany('C', ['a_id', 'location_id'], ['id', 'main_location_id']);
}
Please or to participate in this conversation.