Laravel Collection Sort By Date Example - TechvBlogs

Laravel Collection Sort By Date Example

In this article, You will learn How to Sort Array by Data using Laravel Collection with Example.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

2 months ago

TechvBlogs - Google News

This concise article illustrates how to sort a Laravel collection by date. We will walk through the implementation of sorting a collection based on dates in Laravel. Delve into the Laravel collection order by date concept, particularly focusing on sorting by date in descending order. The tutorial provides a straightforward step-by-step guide on sorting a Laravel collection by date, offering a clear example for easy comprehension.

Laravel Collection Sort By Date Example

Sorting a Laravel collection by date is effortlessly achieved using the sortBy() method. Below, I present two straightforward examples along with their respective outputs, providing a clear understanding of the process. Let's explore these simple examples:

Laravel Collection Sort By Date Example 1:

You can see the below controller code:

<?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $collection = collect([
            ['id' => 1, 'name' => 'Smit', 'created_at' => '2023-11-06'],
            ['id' => 2, 'name' => 'Suresh', 'created_at' => '2023-11-10'],
            ['id' => 3, 'name' => 'Vishal', 'created_at' => '2023-11-05'],
            ['id' => 4, 'name' => 'Bhavik', 'created_at' => '2023-11-04'],
        ]);
  
        $sorted = $collection->sortBy('created_at');
  
        $sorted = $sorted->all();
      
        dd($sorted);
    }
}

Output:

Array
(
    [3] => Array
        (
            [id] => 4
            [name] => Bhavik
            [created_at] => 2023-11-04
        )
    [0] => Array
        (
            [id] => 3
            [name] => Vishal
            [created_at] => 2023-11-05
        )
    [2] => Array
        (
            [id] => 1
            [name] => Smit
            [created_at] => 2023-11-06
        )
    [1] => Array
        (
            [id] => 2
            [name] => Suresh
            [created_at] => 2023-11-10
        )
)

 Laravel Collection Sort By Date Example 2:

You can see the below controller code:

<?php
     
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $collection = collect(['2023-11-06', '2023-11-10', '2023-11-05', '2023-11-04']);
    
        $sorted = $collection->sortBy(function ($date) {
            return \Carbon\Carbon::createFromFormat('Y-m-d', $date);
        });
      
        $sorted = $sorted->all();
      
        dd($sorted);
    }
}

Output:

Array
(
    [3] => 2023-11-04
    [2] => 2023-11-05
    [0] => 2023-11-06
    [1] => 2023-11-10
)

Thank you for reading this article!

Comments (0)

Comment


Note: All Input Fields are required.