具有 Pivot -> children 的嵌套项目不获取数据透视表

Nested items with Pivot -> children do not get the pivot data

我正在尝试为我的 user 模型构建嵌套 items 的可能性。

实际上,每个user可以有很多items,它们可以有自己类型的children(item)。

这是用户与item的关系:

   /**
     * @return \Illuminate\Database\Eloquent\Relations\belongsToMany
     */
    public function items()
    {
        return $this->belongsToMany(Item::class)->withPivot( 'value'');
    }

children 是这样解决的:

   /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function children()
    {
        return $this->hasMany(Item::class, 'parent_id');
    }

现在 - 感谢您之前的帮助 - 我可以使用 children:

查询项目
$user->items()->whereNull('parent_id')->with('children')->get();

这是结果:

Illuminate\Database\Eloquent\Collection {#3247
 all: [
   App\Item {#3232
     id: 2,
     parent_id: null,
     title: "Parent 1",

     pivot: Illuminate\Database\Eloquent\Relations\Pivot {#3235
       user_id: 12,
       user_item_id: 2,
       value: "3",
     },
     children: Illuminate\Database\Eloquent\Collection {#3267
       all: [
         App\Item {#3270
           id: 3,
           parent_id: 2,
           title: "Child 1",
           created_at: "2019-10-04 14:29:59",
           updated_at: "2019-10-04 14:29:59",
         },
         App\Item {#3272
           id: 4,
           parent_id: 2,
           title: "Child 2",
           created_at: "2019-10-04 14:30:16",
           updated_at: "2019-10-04 14:30:16",
         },
       ],
     },
   },
   App\Item {#3255
     id: 5,
     parent_id: null,
     title: "Parent 2",
     created_at: "2019-10-04 14:36:50",
     updated_at: "2019-10-04 14:36:50",
     pivot: Illuminate\Database\Eloquent\Relations\Pivot {#3240
       user_id: 12,
       user_item_id: 5,
       value: "50",
     },
     children: Illuminate\Database\Eloquent\Collection {#3266
       all: [],
     },
   },
 ],
}

如您所见,parent 项有一个枢轴值 - children 没有任何枢轴数据。我该如何解决?我了解到,hasMany 不提供枢轴。

我的问题

在这种情况下如何将数据透视表添加到嵌套 children

如您所说,您已经在 user 和 parent item 之间检索枢轴。这就是您获得数据透视属性的原因:

Illuminate\Database\Eloquent\Collection {#3247
 all: [
   App\Item {#3232
     id: 2,
     parent_id: null,
     title: "Parent 1", // <-----------

     pivot: Illuminate\Database\Eloquent\Relations\Pivot { /** ... */ }

现在,您急于加载查询的 Itemschildren() 关系,这是一个 hasMany 关系,这就是为什么那些记录没有数据透视元素的原因。

如果您需要输出此数据,您可以只显示每个 child 的 parent 记录。现在,如果您真的需要将 parent 主数据添加到那些 children 记录,您可以复制 parent 映射检索到的集合的数据并将数据添加到每个 child元素:

$users = $user->items()->whereNull('parent_id')->with('children')->get();

$users = $users->map(function ($user) {
    $user->items->map(function ($parent) {
        $parent->children->map(function ($child) use ($parent) {
            $child->attribute = $parent->attribute;

            return $child;
        });

        return $parent;
    });

    return $user;
});

在此示例中,现在每个 child Item 都会在其中复制 parent attribute。此解决方案的缺点是 parent 数据将与添加到 children 数据的数据相同,但我不知道这是否是您想要的输出。

此外,请记住所有这些过程都是在内存中进行的,因此您应该始终控制检索的数据量以避免内存问题。

另一种方法是将children Items 直接关联到User objects。这样,每个 child 项目都会有自己的数据透视表,您不需要急切地在 parent Items 上加载 children() 关系。当然,您可以将它们分组以获得适合您需要的层次结构,或者只是将 parent 和它们的 children 与 parent_id.

的值相差