找到你要的答案

Q:Laravel Multidimensional array from database

Q:laravel多维数组从数据库

I have a little problem with laravel. this is my database :

I need a array like this :

array(x) {

    [22] array(2) {
            array ( ["id"] => 16, ["name"] => Charente),
            array ( ["id"] => 17, ["name"] => Charent-Maritime),
    }

    [21] array(1) {
            array ( ["id"] => 1, ["name"] => Ain),
     }
     ...
}

This is an array, where for each region, we have the list of department belonging to the region.

What is the syntax to get this ? Actually i use this :

    return $this->model
    ->select('id', 'name', 'id_region')->get();

But this is not what i want x)

Sorry for my bad english. And thank you by advance.

我与laravel,有一个小问题。这是我的数据库:

我需要一个这样的数组:

array(x) {

    [22] array(2) {
            array ( ["id"] => 16, ["name"] => Charente),
            array ( ["id"] => 17, ["name"] => Charent-Maritime),
    }

    [21] array(1) {
            array ( ["id"] => 1, ["name"] => Ain),
     }
     ...
}

这是一个数组,对于每个区域,我们有属于该区域的部门列表。

What is the syntax to get this ? Actually i use this :

    return $this->model
    ->select('id', 'name', 'id_region')->get();

但这不是我想要的X)

Sorry for my bad english. And thank you by advance.

answer1: 回答1:

Use the collection's groupBy method:

return $this->model->all()->groupBy('id_region');

使用收集的分组方法:

return $this->model->all()->groupBy('id_region');
arrays  database  laravel