D
D
dk-web2015-11-14 23:42:26
Laravel
dk-web, 2015-11-14 23:42:26

How to group data from Mysql table in Laravel 5.1?

I figured out the table relationships and everything works out well, but I stumbled on the banal (
How to make two-dimensional arrays from one table .. sorry (
Actually there is a simple table - house id, street, house number
You need to get a two-dimensional array
street house id
1 house
id 2
like this Is it better to do it using Laravel?
As I understand it, you need to make a selection of the entire table - will you get a collection and already work with it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2015-11-15
@dk-web

Everything is very simple:

// ...
$result = MySuperTable::orderBy('blabla')->get()->groupBy('street');

$result will contain an object, call $result->all(); and get an array:
[
  'street1' => [house1, house2],
  'street2' => [house3, house4],
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question