[英]Change 'descendants' relationship to 'siblings' relationship
I have variants
table as following: 我有
variants
表如下:
+-------------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| parent_product_id | int(10) unsigned | NO | MUL | NULL | |
| child_product_id | int(10) unsigned | NO | MUL | NULL | |
+-------------------+------------------+------+-----+---------------------+----------------+
with constraints: 有约束:
CONSTRAINT `variant_products_child_product_id_foreign` FOREIGN KEY (`child_product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE
CONSTRAINT `variant_products_parent_product_id_foreign` FOREIGN KEY (`parent_product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE
Let's say that it is filled with: 让我们说它充满了:
| id | parent_product_id | child_product_id |
|----+-------------------+------------------|
| 28 | 9 | 11 |
| 29 | 17 | 30 |
| 30 | 9 | 59 |
| 31 | 9 | 60 |
| 32 | 17 | 25 |
At first, business requirements was that one (parent) product can have multiple children. 起初,业务要求是一个(父)产品可以有多个子项。 In my
Product
model I have 在我的
Product
型号中我有
public function variants()
{
return $this->hasMany(\App\Variant::class, 'parent_product_id', 'id');
}
And in Variant
model: 在
Variant
模型中:
public function child()
{
return $this->belongsTo(\App\Product::class, 'child_product_id');
}
When I am querying Product
(id:9) using: 当我使用以下方法查询
Product
(id:9)时:
$query->with([
'variants.child' => function ($query) {
$query->select(['products.id', 'products.name'])
},
]);
I am getting nice response: 我得到了很好的回应:
{
"id": 9,
"name": "Foo",
"description": "Ipsam minus provident cum accusantium id asperiores.",
"variants": [
{
"id": 11,
"name": "Bar"
},
{
"id": 59,
"name": "Fizz"
},
{
"id": 60,
"name": "Buzz"
}
]
}
When asking about product 59
, there are not any variants. 在询问产品
59
,没有任何变体。
Now, I need to redefine my relationships so that products and variants will become rather siblings than descendants . 现在,我需要重新定义我的关系,以便产品和变体成为兄弟姐妹,而不是后代 。
For example, after asking about product 59, desired response is: 例如,在询问产品59之后,所需的响应是:
{
"id": 59,
"name": "Fizz",
"description": "Lorem ipsum dolor sit amet, consectetur adipisicing elit.",
"variants": [
{
"id": 9,
"name": "Foo"
},
{
"id": 11,
"name": "Bar"
},
{
"id": 60,
"name": "Buzz"
}
]
}
How can I achieve it without changing database structure. 如何在不改变数据库结构的情况下实现它。 Any help and tips are much appreciated.
非常感谢任何帮助和提示。
Edit : Two notes: 编辑 :两个笔记:
child_product_id
column in database.) child_product_id
列有一个唯一键。) As I said in the comment, I think it is still better to keep some type of parent to better manage the sibling relationships. 正如我在评论中所说,我认为保留某种类型的父母以更好地管理兄弟关系仍然更好。 For example if you want 59, 9, 11, 60 to be siblings, you can keep a common parent id for them all, like 999, which will keep them as siblings.
例如,如果你想让59,9,11,60成为兄弟姐妹,你可以为它们保留一个共同的父ID,比如999,这将使它们成为兄弟姐妹。
The other thing is that, if you have only one parent_product_id
for each item, you don't need to keep it in a separate table. 另一件事是,如果每个项目只有一个
parent_product_id
,则不需要将其保存在单独的表中。 You can keep the parent_product_id
in the same table products
and set variants in \\App\\Product
like this: 您可以将
parent_product_id
保留在相同的表products
并在\\App\\Product
设置变体,如下所示:
public function variants()
{
return $this->hasMany(\App\Product::class, 'parent_product_id', 'parent_product_id');
}
Now, you can get the list of siblings with this little modification to your query part : 现在,您可以通过对查询部分进行这一点修改来获取兄弟姐妹列表:
$query->with([
'variants' => function ($query) {
$query->select(['products.id', 'products.name'])
},
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.