Laravel Groupby并采取一些价值观

发布于 2025-02-13 18:05:27 字数 802 浏览 1 评论 0原文

我正在使用Laravel 8.X和建筑REST API项目。 我被询问了。

我有一个桌子

ID名称类型颜色
1动物黑色
2动物黄色
3动物红色
4动物白色

我想做这样的事情,

$ andy-&groupby('name'') - > get; get; ()

,希望得到结果,

$animals=[
          {
           name: "dog",
           type: "animal",
           colors: ["black", "yellow"]
          },
          {
           name: "cat",
           type: "animal",
           colors: ["red", "white"]
          }
         ]

有人帮我吗?

I am using laravel 8.x and building rest API project.
I am stuck with a query.

I have a table

idnametypecolor
1Doganimalblack
2Doganimalyellow
3Catanimalred
4Catanimalwhite

I want to do something like that,

$animals->groupBy('name')->get()

and hope to get result like,

$animals=[
          {
           name: "dog",
           type: "animal",
           colors: ["black", "yellow"]
          },
          {
           name: "cat",
           type: "animal",
           colors: ["red", "white"]
          }
         ]

Anyone help me?

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

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

发布评论

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

评论(2

网白 2025-02-20 18:05:27

我将稍微改善Erik答案。

        $animals = Animals::all()->groupBy('name')->map(function($item) {
            return [
                'name' => $item[0]['name'], // the name always the same
                'type' => $item[0]['type'], // if the type not change (as the name)
                'colors' => $item->pluck('color')->unique()->toArray(),
            ];
        })->values()->toArray();

I will slightly improve the Erik answer.

        $animals = Animals::all()->groupBy('name')->map(function($item) {
            return [
                'name' => $item[0]['name'], // the name always the same
                'type' => $item[0]['type'], // if the type not change (as the name)
                'colors' => $item->pluck('color')->unique()->toArray(),
            ];
        })->values()->toArray();
看透却不说透 2025-02-20 18:05:27

我将使用 laravel maptogroups collection方法

$data = Animal::get();

$animals = collect($data)->mapToGroups(function ($item) {
            return [$item['name'] => $item];
        })->map(function ($itemGoup, $name) {
            return [
                'name' => $name,
                'type' => data_get($itemGoup, '0.type'),
                'colors' => collect($itemGoup)->pluck('color')->unique()->values()->toArray(),
            ];
        })->values()->toArray();

带有输出:

[
    {
        "name": "Dog",
        "type": "animal",
        "colors": ["black", "yellow"]
    },
    {
        "name": "Cat",
        "type": "animal",
        "colors": ["red", "white"]
    }
]

I would use Laravel mapToGroups collection method

$data = Animal::get();

$animals = collect($data)->mapToGroups(function ($item) {
            return [$item['name'] => $item];
        })->map(function ($itemGoup, $name) {
            return [
                'name' => $name,
                'type' => data_get($itemGoup, '0.type'),
                'colors' => collect($itemGoup)->pluck('color')->unique()->values()->toArray(),
            ];
        })->values()->toArray();

with output:

[
    {
        "name": "Dog",
        "type": "animal",
        "colors": ["black", "yellow"]
    },
    {
        "name": "Cat",
        "type": "animal",
        "colors": ["red", "white"]
    }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文