【发布时间】:2021-12-01 20:10:00
【问题描述】:
laravel 开发。
我有三个 表,比如说 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 enrty- 1,2,3**
-total_qty
-aval_qty
关系:
表A有很多B
表A也有很多C
查询:
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();
我不明白如何从表 C 到表 A
获取 location_id、total_qty、aval_qty其中有 c.location_id = a.main_location_id
连我自己都不知道自己做得对不对?
这可能吗?
请帮帮我
非常感谢!
【问题讨论】:
-
你必须查询tabler C并用查询过滤它,查询是指A。示例
$c = C::with(['A' => fn ($query) => $query->where('id', '=', 'a_id')])->get();见laravel.com/docs/8.x/…
标签: laravel one-to-many eager-loading has-many