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();
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
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');
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community