E
E
edleman2019-10-31 09:34:40
MySQL
edleman, 2019-10-31 09:34:40

Laravel get data array from db sorted by table columns?

There is a table in the database with the following data
['id', 'user_id', 'date', 'number', 'text']
['1', '1', '2019-10-30', '11', ' bla bla bla 1']
['2', '1', '2019-10-30', '12', 'bla bla bla 2']
['3', '1', '2019-10-31' , '11', 'bla bla bla 3']
Is it possible to get the following result when querying this table in the database
[
'2019-10-30' => [
'11' => ['1', '2019- 10-30', '11', 'bla bla bla 1']
'12' => ['2', '2019-10-30', '12', 'bla bla bla 2']
]
'2019-10-31' => [
'11' => ['3', '2019-10-31', '11', 'bla bla bla 3']
]
]
those. the key will be the name of the column, but the array in the array
is something like this, but 2 keyBy is needed by date and number
$query = Post::where('user_id', 1)->get()->keyBy('date' )->toArray();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2019-10-31
@edleman

->keyBy('date')->map->keyBy('number')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question