1

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

enter image description here

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.

1 Answer 1

3

Use the collection's groupBy method:

return $this->model->all()->groupBy('id_region');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.