🚗🏍️ Welcome to Motoshare!

Turning Idle Vehicles into Shared Rides & New Earnings.
Why let your bike or car sit idle when it can earn for you and move someone else forward?

From Idle to Income. From Parked to Purpose.
Earn by Sharing, Ride by Renting.
Where Owners Earn, Riders Move.
Owners Earn. Riders Move. Motoshare Connects.

With Motoshare, every parked vehicle finds a purpose. Partners earn. Renters ride. Everyone wins.

Start Your Journey with Motoshare

Example of Laravel Collection Sort By Date

Laravel

Here’s a detailed tutorial on how to sort a Laravel Collection by date with examples.

Example of Laravel Collection Sort By Date

In Laravel, you can easily sort collections by date using the sortBy method. This method allows you to sort your collection based on the value of a particular field, such as a date field. Below, I’ll show you how to work with a collection and sort it by a date field.

Step 1: Sample Data

Let’s assume you have a collection of blog posts, each with a title and a created_at date:

use Illuminate\Support\Collection;

// Sample data
$posts = collect([
    ['title' => 'Post One', 'created_at' => '2023-08-01 14:00:00'],
    ['title' => 'Post Two', 'created_at' => '2023-06-15 10:30:00'],
    ['title' => 'Post Three', 'created_at' => '2023-09-25 16:45:00'],
    ['title' => 'Post Four', 'created_at' => '2023-07-05 09:00:00'],
]);

Step 2: Sorting by Date

You can sort this collection by the created_at date using the sortBy method. Here’s how:

$sortedPosts = $posts->sortBy('created_at');

The sortBy method will sort the collection in ascending order by default.

Step 3: Displaying the Sorted Collection

Let’s display the sorted posts:

$sortedPosts->each(function ($post) {
    echo "Title: {$post['title']}, Created At: {$post['created_at']}\n";
});

Output:

Title: Post Two, Created At: 2023-06-15 10:30:00
Title: Post Four, Created At: 2023-07-05 09:00:00
Title: Post One, Created At: 2023-08-01 14:00:00
Title: Post Three, Created At: 2023-09-25 16:45:00

Sorting in Descending Order

If you want to sort the collection by date in descending order, use the sortByDesc method:

$sortedPostsDesc = $posts->sortByDesc('created_at');

$sortedPostsDesc->each(function ($post) {
    echo "Title: {$post['title']}, Created At: {$post['created_at']}\n";
});

Output:

Title: Post Three, Created At: 2023-09-25 16:45:00
Title: Post One, Created At: 2023-08-01 14:00:00
Title: Post Four, Created At: 2023-07-05 09:00:00
Title: Post Two, Created At: 2023-06-15 10:30:00

Explanation

  • sortBy('created_at'): Sorts the collection by the created_at field in ascending order.
  • sortByDesc('created_at'): Sorts the collection by the created_at field in descending order.
  • each: Iterates over the collection and prints each post’s title and created_at date.

This example demonstrates how easy it is to sort Laravel collections by date using the sortBy and sortByDesc methods.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x