【问题标题】:如何在 Laravel 中按孩子分组收藏?
【发布时间】:2022-01-21 12:46:48
【问题描述】:

我有一个集合“功能”,每个都有一个“功能组”。 1 个功能 belongsToMany 功能组。

1 个功能 belongsToMany 汽车,因此它本身就是一个子集合。在我的汽车视图中,我想对功能进行分组。我该怎么办?

示例集合:

[
   {
      "id":1,
      "group_id":1,
      "name":"metallic",
      "pivot":{
         "car_id":1,
         "feature_id":1
      },
      "group":{
         "id":1,
         "name":"paint"
      }
   },
   {
      "id":2,
      "group_id":1,
      "name":"blue",
      "pivot":{
         "car_id":1,
         "feature_id":2
      },
      "group":{
         "id":1,
         "name":"paint"
      }
   },
   {
      "id":5,
      "group_id":2,
      "name":"gps",
      "pivot":{
         "car_id":1,
         "feature_id":5
      },
      "group":{
         "id":2,
         "name":"interior"
      }
   },
   {
      "id":6,
      "group_id":2,
      "name":"leather seats",
      "pivot":{
         "car_id":1,
         "feature_id":6
      },
      "group":{
         "id":2,
         "name":"interior"
      }
   }
]

想要的结果应该是这样的:

[
   {
      "id":1,
      "name":"paint",
      "features":[
         {
            "id":1,
            "group_id":1,
            "name":"metallic"
         },
         {
            "id":2,
            "group_id":1,
            "name":"blue"
         }
      ]
   },
   {
      "id":2,
      "name":"interior",
      "features":[
         {
            "id":5,
            "group_id":2,
            "name":"gps"
         },
         {
            "id":6,
            "group_id":2,
            "name":"leather seats"
         }
      ]
   }
]

在我的控制器中,我正在尝试使用each() 循环和mergeRecursive() 创建一个新集合,如下所示:

$car = Car::where('slug', $slug)->first()->load('features.group');

$newCollection = collect(["group 1" => "feature 1"]);

$car->features->each(function ($item, $key) use($newCollection) {
    $current = collect([
        $item->group->name => $item->name,
    ]);
    $newCollection->mergeRecursive($current);
});

dd($newCollection);

return view('pages.car', [
    'car' => $car,
    'features' => $newCollection,
]);

我的集合中唯一的数据是我实例化它的虚拟数据。我错过了什么?

有什么建议吗?谢谢!

【问题讨论】:

    标签: laravel eloquent laravel-8


    【解决方案1】:

    好的,我得到了解决方案。无需遍历集合来创建新集合。有一个收集方法groupBy() 可以解决问题:

    $myCollection->groupBy('group_id'); 返回:

    {
       "1":[
          {
             "id":1,
             "group_id":1,
             "name":"metallic",
             "pivot":{
                "car_id":1,
                "feature_id":1
             },
             "group":{
                "id":1,
                "name":"paint"
             }
          },
          {
             "id":2,
             "group_id":1,
             "name":"blue",
             "pivot":{
                "car_id":1,
                "feature_id":2
             },
             "group":{
                "id":1,
                "name":"paint"
             }
          }
       ],
       "2":[
          {
             "id":5,
             "group_id":2,
             "name":"gps",
             "pivot":{
                "car_id":1,
                "feature_id":5
             },
             "group":{
                "id":2,
                "name":"interior"
             }
          },
          {
             "id":6,
             "group_id":2,
             "name":"leather seats",
             "pivot":{
                "car_id":1,
                "feature_id":6
             },
             "group":{
                "id":2,
                "name":"interior"
             }
          }
       ]
    }
    

    或者我可以使用嵌套键,$myCollection->groupBy('group.name'); 返回:

    {
       "paint":[
          {
             "id":1,
             "group_id":1,
             "name":"metallic",
             "pivot":{
                "car_id":1,
                "feature_id":1
             },
             "group":{
                "id":1,
                "name":"paint"
             }
          },
          {
             "id":2,
             "group_id":1,
             "name":"blue",
             "pivot":{
                "car_id":1,
                "feature_id":2
             },
             "group":{
                "id":1,
                "name":"paint"
             }
          }
       ],
       "interior":[
          {
             "id":5,
             "group_id":2,
             "name":"gps",
             "pivot":{
                "car_id":1,
                "feature_id":5
             },
             "group":{
                "id":2,
                "name":"interior"
             }
          },
          {
             "id":6,
             "group_id":2,
             "name":"leather seats",
             "pivot":{
                "car_id":1,
                "feature_id":6
             },
             "group":{
                "id":2,
                "name":"interior"
             }
          }
       ]
    }
    

    现在可以在刀片布局/组件中使用@foreach() 循环输出:

    @foreach ($features as $feature)
        <h3>{{ $feature[0]->group->name }}</h3>
        @foreach ($feature as $item)
            <div>{{ $item->name }}</div>
        @endforeach
    @endforeach
    

    【讨论】:

      猜你喜欢
      • 2014-10-31
      • 1970-01-01
      • 2019-12-17
      • 1970-01-01
      • 2021-08-08
      • 2018-09-01
      • 2014-01-01
      • 2020-10-23
      • 1970-01-01
      相关资源
      最近更新 更多