latest()
is an Eloquent method for queries that essentially asks the database to orderBy('created_at', 'desc')
.
Unfortunately, this method does not exist on the Laravel collection (thus the error message).
You can, however, make this work the way you want, using a different method on your collection.
Try:
return collect($this->messages)->sortBy('created_at');
You can get pretty crafty with sortBy
, creating virtually anything you want using a closure:
$x = $collection->sortBy(function ($product, $key) {
return count($product['colors']);
});
The base method also takes a variety of standard PHP sort flags to make life easy. E.g.:
$collection->sortBy('title', SORT_NATURAL);
* Be the first to Make Comment