@hal.littel
In Laravel, you can use the sortBy()
or sortByDesc()
methods to sort an array of data.
1 2 3 4 5 |
$collection = Model::all(); $sortedCollection = $collection->sortBy('column_name'); // Sort in ascending order // OR $sortedCollection = $collection->sortByDesc('column_name'); // Sort in descending order |
1 2 3 4 5 |
$array = ['John', 'Jane', 'Alice', 'Bob']; $sortedArray = collect($array)->sortBy(); // OR $sortedArray = collect($array)->sortByDesc(); |
By using these methods, you can easily sort your data in Laravel.