I need to get last 10 records of a table ordered by a data, and reverse them.
This is the code before the reverse:
$eventi = \App\Model::with('relation_1', 'relation_2')
->orderBy('data_ora', 'desc')
->take(10)
->get();
If I log the results I get this:
[{"id":12297,"stato_batteria":null,"data_ora":"2018-05-03 11:40:02" ...
The reverse code is:
$eventi = \App\Model::with('relation_1', 'relation_2')
->orderBy('data_ora', 'desc')
->take(10)
->get()
->reverse();
If I log the results I get this:
{"9":{"id":1410,"stato_batteria":null,"data_ora":"2018-04-05 14:16:48" ...
As you can see the collection is changed and I do not know why.
reverse
which returns a new collection with the items in reverse order. – Plumbiferous[{"id":12297,"stato_batteria":null
and{"9":{"id":1410,"stato_batteria"
are pretty different from the beginning. The first one is a json array, the second one is a json object. – Disentanglereverse
is preserving keys (which initially were numeric). You're seeing expected behaviour I believe but I need to see more data to be able to comment. – Plumbiferous