Laravel Collection SortBy Tutorial with Examples
In this post we will give you information about Laravel Collection SortBy Tutorial with Examples. Hear we will give you detail about Laravel Collection SortBy Tutorial with ExamplesAnd how to use it also give you demo for it if it is necessary.
Hi Artisan,
If you need to see example of laravel collection sort by example. i explained simply step by step laravel collection sort by two columns. we will help you to give example of laravel collection sort by date. step by step explain laravel collection sort by name.
I will give you list of examples of sort by colletion in laravel. so you can easily use it with your laravel 5, laravel 6 and laravel 7 application. so let’s see bellow example that will helps you lot.
1) Example 1: Laravel Collection Sort By Simple Example
2) Example 2: Laravel Collection Sort By Count
3) Example 3: Laravel Collection Sort By Date
4) Example 4: Laravel Collection Sort By Two Fields
5) Example 5: Laravel Collection Sort By Relation
Example 1: Laravel Collection Sort By Simple Example
public function index()
{
$collection = collect([
['id' => 1, 'name' => 'Hardik', 'email' => 'hardik@gmail.com'],
['id' => 2, 'name' => 'Ankit', 'email' => 'ankit@gmail.com'],
['id' => 3, 'name' => 'Balo', 'email' => 'balo@gmail.com'],
]);
$sorted = $collection->sortBy('name');
$sorted->all();
dd($sorted);
}
Output:
IlluminateSupportCollection Object
(
[items:protected] => Array
(
[1] => Array
(
[id] => 2
[name] => Ankit
[email] => ankit@gmail.com
)
[2] => Array
(
[id] => 3
[name] => Balo
[email] => balo@gmail.com
)
[0] => Array
(
[id] => 1
[name] => Hardik
[email] => hardik@gmail.com
)
)
)
Example 2: Laravel Collection Sort By Count
public function index()
{
$collection = collect([
['id' => 1, 'name' => 'Vivo', 'models' => ['v11', 'v85']],
['id' => 2, 'name' => 'Appo', 'models' => ['a23']],
['id' => 3, 'name' => 'Apple', 'models' => ['s5', 's6', 's7']],
]);
$sorted = $collection->sortBy(function ($product, $key) {
return count($product['models']);
});
$sorted->all();
dd($sorted);
}
Output:
IlluminateSupportCollection Object
(
[items:protected] => Array
(
[1] => Array
(
[id] => 2
[name] => Appo
[models] => Array
(
[0] => a23
)
)
[0] => Array
(
[id] => 1
[name] => Vivo
[models] => Array
(
[0] => v11
[1] => v85
)
)
[2] => Array
(
[id] => 3
[name] => Apple
[models] => Array
(
[0] => s5
[1] => s6
[2] => s7
)
)
)
)
Example 3: Laravel Collection Sort By Date
public function index()
{
$collection = collect([
['id' => 1, 'name' => 'Hardik', 'created_date' => '2020-04-05'],
['id' => 2, 'name' => 'Ankit', 'created_date' => '2020-04-01'],
['id' => 3, 'name' => 'Balo', 'created_date' => '2020-04-03'],
]);
$sorted = $collection->sortBy('created_date');
$sorted->all();
dd($sorted);
}
Output:
IlluminateSupportCollection Object
(
[items:protected] => Array
(
[0] => Array
(
[id] => 1
[name] => Hardik
[created_date] => 2019-04-05
)
[1] => Array
(
[id] => 2
[name] => Ankit
[created_date] => 2020-04-01
)
[2] => Array
(
[id] => 3
[name] => Balo
[created_date] => 2020-04-03
)
)
)
Example 4: Laravel Collection Sort By Two Fields
public function index()
{
$collection = collect([
['id' => 1, 'name' => 'Hardik', 'city' => 'Mumbai'],
['id' => 2, 'name' => 'Ankit', 'city' => 'Rajkot'],
['id' => 3, 'name' => 'Balo', 'city' => 'Rajkot'],
['id' => 4, 'name' => 'Ankit', 'city' => 'Mumbai'],
]);
$sorted = $collection->sortBy(function ($product, $key) {
return $product['city'].$product['name'];
});
$sorted->all();
dd($sorted);
}
Output:
IlluminateSupportCollection Object
(
[items:protected] => Array
(
[3] => Array
(
[id] => 4
[name] => Ankit
[city] => Mumbai
)
[0] => Array
(
[id] => 1
[name] => Hardik
[city] => Mumbai
)
[1] => Array
(
[id] => 2
[name] => Ankit
[city] => Rajkot
)
[2] => Array
(
[id] => 3
[name] => Balo
[city] => Rajkot
)
)
)
Example 5: Laravel Collection Sort By Relation
public function index()
{
$posts = Post::get()->sortBy(function($query){
return $query->auther->name;
})
->all();
}
I hope it can help you…
Hope this code and post will helped you for implement Laravel Collection SortBy Tutorial with Examples. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs