Today, We want to share with you Sort Collection By dates in Laravel.In this post we will show you laravel collection custom sort, hear for laravel collection->sort by value we will give you demo and example for implement.In this post, we will learn about laravel collection->sortby multiple with an example.
Sort Collection By dates in Laravel
There are the Following The simple About laravel collection to array of objects Full Information With Example and source code.
As I will cover this Post with live Working example to develop laravel collection to array of objects, so the some major files and Directory structures for this example is following below.
Laravel is a web application framework with expressive, elegant syntax.The PHP Framework for Web Artisans,freeing you to create without sweating the small things. CRUD Operation With Server Side.
Laravel Collection Sort By Date
Creating an addon: sorting collection by date created
public function index() { $collection = collect([ ['id' => 1, 'name' => 'Hanuman', 'created_date' => '2022-04-05'], ['id' => 2, 'name' => 'abhishek', 'created_date' => '2022-04-01'], ['id' => 3, 'name' => 'Bharat', 'created_date' => '2022-04-03'], ]); $sorted = $collection->sortBy('created_date'); $sorted->all(); dd($sorted); }
Output:
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => Array ( [id] => 1 [name] => Hanuman [created_date] => 2019-04-05 ) [1] => Array ( [id] => 2 [name] => abhishek [created_date] => 2022-04-01 ) [2] => Array ( [id] => 3 [name] => Bharat [created_date] => 2022-04-03 ) ) )
Web Programming Tutorials Example with Demo
Laravel sort collection by date
To sort a Laravel collection by date, you can use the sortBy or sortByDesc method. Here’s an example of how to sort a collection of Post models by their created_at date in ascending order:
$posts = Post::all()->sortBy('created_at');
In this example, we are calling the all method on the Post model to retrieve all the posts from the database. Then, we are calling the sortBy method on the resulting collection and passing in the name of the created_at field as the sorting criteria. This will sort the collection in ascending order based on the created_at date.
If you want to sort the collection in descending order, you can use the sortByDesc method instead:
$posts = Post::all()->sortByDesc('created_at');
This will sort the collection in descending order based on the created_at date.
Note that if you have a collection of dates stored as strings, you may need to convert them to date objects using the strtotime function before sorting them. Here’s an example:
$dates = collect(['2022-01-01', '2021-01-01', '2023-01-01']); $sortedDates = $dates->map(function ($date) { return strtotime($date); })->sortBy('created_at')->map(function ($date) { return date('Y-m-d', $date); });
In this example, we are first creating a collection of dates as strings. Then, we are using the map method to convert each date string to a Unix timestamp using the strtotime function.
Next, we are using the sortBy method to sort the dates by their Unix timestamps. Finally, we are using the map method again to convert the sorted Unix timestamps back to date strings.
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about laravel’; $collection->sort by key.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.