BelongsToMany 独立子关系 - Laravel/Eloquent

发布于 2025-01-10 14:16:39 字数 1571 浏览 4 评论 0原文

模型结构

FoodCategory - hasMany:Food

Food -belongsToMany:AccessoryGroup |属于:FoodCategory

AccessoryGroup - hasMany:Accessory | belongsToMany:Food

Accessory-belongsTo:AccessoryGroup

$foodCategories = FoodCategory
  ::with([
   'foods',
   'foods.accessory_groups',
   'foods.accessory_groups.accessories',
  ])
  ->get();

$foodCategories->each(function($foodCategory) {
  $foodCategoryId = $foodCategory->id;
  $foodCategory->foods->each(function($food) use( $foodCategoryId ) {
    $food->accessory_groups->each(function($accessoryGroup) use( $foodCategoryId ) {

      $accessoryGroup->accessories->each(function($accessory) use( $foodCategoryId ) {

        // accessory - not unique by different food grandparent realation
        $accessory->parent_food_category_id = $foodCategoryId;

      });

    });
  });
});

问题是当我设置$accessory->parent_food_category_id = $foodCategoryId; 最后一个相同配件覆盖“所有配件”的parent_food_category_id(具有相同的id) - (不同的食物祖父母)

是否可以轻松“克隆”belongsToMany 关系(在本例中是accessory_groups)。与您更改其子项 (accessory) 相比,不要更改所有相同的 accessories (具有相同的 id)

需要优化的解决方案。不要为每种食物调用单独的sql -> accessory_group关系(此问题的解决方案之一)

Model structure:

FoodCategory - hasMany: Food

Food - belongsToMany: AccessoryGroup | belongsTo: FoodCategory

AccessoryGroup - hasMany: Accessory | belongsToMany: Food

Accessory - belongsTo: AccessoryGroup

$foodCategories = FoodCategory
  ::with([
   'foods',
   'foods.accessory_groups',
   'foods.accessory_groups.accessories',
  ])
  ->get();

$foodCategories->each(function($foodCategory) {
  $foodCategoryId = $foodCategory->id;
  $foodCategory->foods->each(function($food) use( $foodCategoryId ) {
    $food->accessory_groups->each(function($accessoryGroup) use( $foodCategoryId ) {

      $accessoryGroup->accessories->each(function($accessory) use( $foodCategoryId ) {

        // accessory - not unique by different food grandparent realation
        $accessory->parent_food_category_id = $foodCategoryId;

      });

    });
  });
});

Problem is when i set $accessory->parent_food_category_id = $foodCategoryId; last same accessory overrides parent_food_category_id value for "all accessories" (with same id) - (diffrent food grandparent).

Is it possible to easily "clone" belongsToMany relation (in this case accessory_groups). And than when you change their child (accessory), to not change on all same accessories (with same id).

Needed optimized solution. Not to call separate sql for every food -> accessory_group relation (One of the solutions for this problem)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱世争霸 2025-01-17 14:16:39

我的解决方案(使用复制模型方法)

$foodCategories->each(function($foodCategory) {
  $foodCategoryId = $foodCategory->id;
  $foodCategory->foods->each(function($food) use( $foodCategoryId ) {
    $food->accessory_groups->each(function($accessoryGroup) use( $foodCategoryId ) {

      $accessoryGroup->accessories->each(function($accessory, $index) use( $foodCategoryId, $accessoryGroup ) {
    
    // Create clone
        $accessoryClone = $accessory->replicate();
        $accessoryClone->parent_food_category_id = $foodCategoryId;
    
    // OVERRIDE existing accessory with cloned
        $accessoryGroup->accessories[$index] = $accessoryClone;

      });

    });
  });
});

My Solution (use replicate Model method)

$foodCategories->each(function($foodCategory) {
  $foodCategoryId = $foodCategory->id;
  $foodCategory->foods->each(function($food) use( $foodCategoryId ) {
    $food->accessory_groups->each(function($accessoryGroup) use( $foodCategoryId ) {

      $accessoryGroup->accessories->each(function($accessory, $index) use( $foodCategoryId, $accessoryGroup ) {
    
    // Create clone
        $accessoryClone = $accessory->replicate();
        $accessoryClone->parent_food_category_id = $foodCategoryId;
    
    // OVERRIDE existing accessory with cloned
        $accessoryGroup->accessories[$index] = $accessoryClone;

      });

    });
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文