【发布时间】:2022-02-23 02:58:20
【问题描述】:
我有这个数据库/模型结构(示例):
*食物类别 ------------ (id, name) - *不相关
食物 ---------------------- (id, food_category_id, name, ...)
配件组 ---------(id、name、...)
Foods_Accessory Groups - (food_id, acc_group_id) - 数据透视表
附件 ---------------- (id, acc_group_id, name, ...)
Foods_Accessories_Prices -(food_id、accessories_id、price) - 数据透视表
模型
- FoodCategory (hasMany: foods)
- 食物(hasMany: foodAccessoriesGroup)
- FoodAccessoriesGroup (hasMany: foodAccessory)
- 有问题:*FoodAccessory(belongsToMany | belongsTo:food_prices | food_price ------ (枢轴:[价格],表格:food_accessory_prices,型号:Food)
**foods**
| id | name |
| -------- | ---- |
| 1 | Pork |
| 2 | Eggs |
**accessory_groups**
| id | name |
| -------| -----|
| 1 | Sauce|
foods_accessory_groups
| food_id | accessory_group_id |
| -------- | -------------- |
| 1 | 1 |
| 2 | 1 |
**accessories**
| id | name | acc_group_id |
| -------- | -------------- | - |
| 1 | Mayo | 1 |
| 2 | Ketchup | 1 |
**foods_accessories_prices**
| food_id | accessory_id | price |
| -------- | -------------- | - |
| 1 | 1 | 200 |
| 2 | 1 | 300 |
| 1 | 2 | 250 |
| 2 | 2 | 350 |
代码示例:
// get all food and relations
$foodCategories = FoodCategory::with([
'foods',
'foods.accessory_groups',
'foods.accessory_groups.accessories',
'foods.accessory_groups.accessories.food_prices' or
'foods.accessory_groups.accessories.food_price' => function ($query) {
// how to filter food prices by great-grandparent relation food (by food_id)
// I know that it can't be done here, but where and how
}
])->get();
配件动态价格取决于食物(曾祖父母关系)。 什么是这个问题的最佳实践和最优化的解决方案? 后处理或 Laravel Eloquent 有解决方案没有太多麻烦
food (id = 1, Pork) -> 有 foods_accessory_groups(id = 1, Sauce) -> 有配件(蛋黄酱和番茄酱),蛋黄酱价格是 200 ,番茄酱的价格是300。
food (id = 2, Eggs) - ... - mayo价格是250,番茄酱价格是350。
API 响应来自示例:
【问题讨论】:
-
向我们展示您的模型并提供您想要实现的示例输出。
-
假设我想要这个 Api 响应。 ibb.co/ngBN0c3 将最后一个支点与他的(深层父母)食物联系起来的最佳做法是什么
-
你不能在那个实例中调用数据透视表,因为该数据透视表在
food <-> accessories之间,并且你正在调用accessories <-> accessoriesgroups之间的关系,如果你直接调用附件,你只能直接调用它食物模型,您需要建立从该数据透视表到通过附件组的食物的关系,建立原始查询可能更容易,但更好地定义模型之间的关系
标签: laravel eloquent relationship has-and-belongs-to-many belongs-to