laravel sort code example
Example 1: laravel order by desc
$posts = Post::orderBy('id', 'DESC')->get();
Example 2: laravel order by
$users = DB::table('users')
-> orderBy('name', 'desc')
-> get();
Example 3: collection laravel filter
$collection = collect([1, 2, 3, 4]);
$filtered = $collection->filter(function ($value, $key) {
return $value > 2;
});
$filtered->all();
// [3, 4]
Example 4: get all sort by laravel
$results = Project::orderBy('name')->get();
Example 5: laravel sort collection
$collection = collect([5, 3, 1, 2, 4]);
$sorted = $collection->sortDesc();
$sorted->values()->all();
// [5, 4, 3, 2, 1]
Example 6: sort laravel eloquent
$posts = Post::orderBy('id', 'DESC')->get();