将 'descendants' 关系更改为 'siblings' 关系

Change 'descendants' relationship to 'siblings' relationship

我有 variants table 如下:

+-------------------+------------------+------+-----+---------------------+----------------+
| 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                |                |
+-------------------+------------------+------+-----+---------------------+----------------+

有约束条件:

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

假设它充满了:

| id | parent_product_id | child_product_id | 
|----+-------------------+------------------| 
| 28 | 9                 | 11               | 
| 29 | 17                | 30               | 
| 30 | 9                 | 59               | 
| 31 | 9                 | 60               | 
| 32 | 17                | 25               | 

一开始业务需求是一个(parent)个产品可以有多个children。在我的 Product 模型中,我有

public function variants()
{
    return $this->hasMany(\App\Variant::class, 'parent_product_id', 'id');
}

并且在 Variant 模型中:

public function child()
{
    return $this->belongsTo(\App\Product::class, 'child_product_id');
}

当我查询 Product (id:9) 使用:

$query->with([
    'variants.child'  => function ($query) {
        $query->select(['products.id', 'products.name'])
    },
]);

我得到了很好的回应:

{
  "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"
    }
  ]
}

当询问产品 59 时,没有任何变体。

现在,我需要重新定义我的关系,以便产品和变体成为兄弟姐妹而不是后代

例如,询问产品 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"
    }
  ]
}

如何在不改变数据库结构的情况下实现它。非常感谢任何帮助和提示。

编辑:两个注释:

正如我在评论中所说,我认为还是保留某种类型的 parent 更好地处理手足关系。例如,如果您希望 59、9、11、60 成为兄弟姐妹,您可以为他们保留一个共同的 parent id,例如 999,这将使他们成为兄弟姐妹。

另一件事是,如果每个项目只有一个 parent_product_id,则不需要将其保存在单独的 table 中。您可以将 parent_product_id 保留在相同的 table products 中,并在 \App\Product 中设置变体,如下所示:

public function variants()
{
    return $this->hasMany(\App\Product::class, 'parent_product_id', 'parent_product_id');
}

现在,您可以通过对查询部分稍作修改来获取兄弟姐妹列表:

$query->with([
    'variants'  => function ($query) {
        $query->select(['products.id', 'products.name'])
    },
]);