Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

There is nothing to order if you are only getting 1 result. The find method looks for a specific id so what is there to order?

If you were to use get and get a collection of calls, you can use orderBy to order the results.

$calls = App\Call::orderBy('created_at')->get();

There is also a latest method, which is just shorthand for orderBy('created').

$calls = App\Call::latest()->get();
0

hello Tomastkim,

I select a call like this:

$ call = App \ Call :: find ($ id);

fill all the data of the call in view

and still need to get the historical atenimento of this call

then do this:

foreach ($ call-> history as $ history)

put it behind me the list in ascending order, and I wanted in decreasing order

Last updated 8 years ago.
0

Ohh, I see what you mean. Is history a related model? There are a few ways to do it.

1 - Eager load it and let mysql sort it (probably the preferred method).

$call = App\Call::with(['history' => function($query) {
    // Sort the history model within this closure
    $query->orderBy('created_at', 'desc');
}])->find(1);

2 - The second option is to just get the results and sort it using PHP / the collection's built-in sorting methods.

$call = App\Call::find(1);
$call->history->sortByDesc('created_at');
Last updated 8 years ago.
0

I prederred the 2 method.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.