【问题标题】:How to create an array inside a Laravel collection如何在 Laravel 集合中创建数组
【发布时间】:2021-02-13 19:41:19
【问题描述】:

我正在使用 Laravel 7。 我需要检索所有有许多团队参与的项目。

这是我在控制器方法中写的代码:

    public function index()
    {
        $projects = Project::select('projects.id as id', 'projects.name as name', 'teams.name as team_name')
            ->leftJoin('tasks', 'projects.id', '=', 'tasks.project_id')
            ->join('task_user', 'tasks.id', '=', 'task_user.task_id')
            ->join('users', 'task_user.user_id', '=', 'users.id')
            ->join('teams', 'users.team_id', '=', 'teams.id')
            ->orderBy('projects.id')
            ->distinct()
            ->get();

        foreach ($projects as $key => $value) {
            $projects_length = $projects->where('id', $value['id'])->count();
            if($projects_length === 1) {
                unset($projects[$key]);
            }
        }

        $projects = $projects->values();

        return $projects;
}

我以$projects 返回的数据格式如下:

[
    {
        "id": 2,
        "name": "Creating new app with Symfony",
        "team_name": "Warriors"
    },
    {
        "id": 2,
        "name": "Creating new app with Symfony",
        "team_name": "Guardians"
    },
    {
        "id": 3,
        "name": "Creating new app with Vue.js",
        "team_name": "Warriors"
    },
    {
        "id": 3,
        "name": "Creating new app with Vue.js",
        "team_name": "Reds"
    }
]

您可以想象,使用类似的响应数据格式是很困难的。 我想把team_name的数据封装成一个数组,如下:

[
    {
        "id": 2,
        "name": "Creating new app with Symfony",
        "team_name": 
            [
                "Warriors",
                "Guardians"
            ]
    },
    {
        "id": 3,
        "name": "Creating new app with Vue.js",
        "team_name": 
            [
                "Warriors",
                "Reds"
            ]
    }
]

可以帮忙吗?

【问题讨论】:

    标签: laravel collections


    【解决方案1】:
    collect([
        [
            "id"=> 2,
            "name"=> "Creating new app with Symfony",
            "team_name"=> "Warriors"
        ],
        [
            "id"=> 2,
            "name"=> "Creating new app with Symfony",
            "team_name"=> "Guardians"
        ],
        [
            "id"=> 3,
            "name"=> "Creating new app with Vue.js",
            "team_name"=> "Warriors"
        ],
        [
            "id"=> 3,
            "name"=> "Creating new app with Vue.js",
            "team_name"=> "Reds"
        ]
    ])->groupBy('id')->map(function($data, $id) {
      return [
        'id' => $id,
        'name' => $data[0]['name'],
        'team_nameS' => $data->pluck('team_name')->toArray()
      ];
    })->values()->toArray()
    

    一种方法。也许您可以考虑使用 eloquent 方法 withwhereHas 等来解决大部分问题。

    输出

    => [
         [
           "id" => 2,
           "name" => "Creating new app with Symfony",
           "team_nameS" => [
             "Warriors",
             "Guardians",
           ],
         ],
         [
           "id" => 3,
           "name" => "Creating new app with Vue.js",
           "team_nameS" => [
             "Warriors",
             "Reds",
           ],
         ],
       ]
    

    【讨论】:

      【解决方案2】:

      您可以尝试在您的 eloquent 中使用 GROUP_CONCAT 和 group by。我没有在我的查询浏览器上运行它,但您可以验证一次并共享日志,以检查任何进一步的错误。

          $projects = Project::select('projects.id as id')
              ->leftJoin('tasks', 'projects.id', '=', 'tasks.project_id')
              ->join('task_user', 'tasks.id', '=', 'task_user.task_id')
              ->join('users', 'task_user.user_id', '=', 'users.id')
              ->join('teams', 'users.team_id', '=', 'teams.id')
              ->selectRaw('GROUP_CONCAT(projects.name) as name', 'GROUP_CONCAT(teams.name) as team_name')
              ->groupBy('projects.id')
              ->orderBy('projects.id')
              ->get();
      

      【讨论】:

      • 我收到以下错误:TypeError: Argument 2 passed to Illuminate\Database\Query\Builder::selectRaw() must be of the type array, string given。顺便问一下,您的查询是否存在关于 SQL 注入的风险?
      猜你喜欢
      • 1970-01-01
      • 2016-07-28
      • 2022-01-20
      • 2020-07-03
      • 2019-02-19
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      相关资源
      最近更新 更多