How to SortBy Collection in Laravel 7|8

Last updated on by Digamber

In this tutorial, we are going to learn how to work with Laravel SortBy query to sort the collection.

We will use by sorting two columns in laravel, and mainly we will focus on laravel collection sort by date and name.

This way, you can filter data in laravel pretty easily and intact the queries performance top-notch.

Here is the complete list for Collection Sort examples that we are going to explain step by step in this tutorial:

  • Collection Sort By Name
  • Collection Sort By Two Fields in Laravel
  • Collection Sort By Date in Laravel
  • Collection Sort By Count in Laravel
  • Collection Sort By Relation in Laravel

Laravel Collection Sort By Name Example

public function index()
{
    $collection = collect([
        ['id' => 1, 'name' => 'John', 'email' => 'john@gmail.com'],
        ['id' => 2, 'name' => 'Sam', 'email' => 'sam@gmail.com'],
        ['id' => 3, 'name' => 'Bilal', 'email' => 'bilal@gmail.com'],
        ['id' => 4, 'name' => 'Lisa', 'email' => 'lisa@gmail.com'],
        ['id' => 5, 'name' => 'Samuel', 'email' => 'samuel@gmail.com']
    ]);
  
    $filterd = $collection->sortBy('name');
  
    $filterd->all();
    
    print_r($filterd);
}

Collection Sort By Two Fields in Laravel

public function index()
{
    $collection = collect([
        ['id' => 1, 'name' => 'John', 'email' => 'john@gmail.com'],
        ['id' => 2, 'name' => 'Sam', 'email' => 'sam@gmail.com'],
        ['id' => 3, 'name' => 'Bilal', 'email' => 'bilal@gmail.com'],
        ['id' => 4, 'name' => 'Lisa', 'email' => 'lisa@gmail.com'],
        ['id' => 5, 'name' => 'Samuel', 'email' => 'samuel@gmail.com']        
    ]);
    
    $filterd = $collection->sortBy(function ($data, $key) {
        return $data['name'].$data['email'];
    });

    $filterd->all();

    print_r($filterd);
}

Collection Sort By Date in Laravel

public function index()
{
    $collection = collect([
        ['id' => 1, 'name' => 'John', 'created_date' => '2021-03-04'],
        ['id' => 2, 'name' => 'Sam', 'created_date' => '2021-02-03'],
        ['id' => 3, 'name' => 'Bilal', 'created_date' => '2020-01-02'],
        ['id' => 4, 'name' => 'Lisa', 'created_date' => '202019-05-01'],
        ['id' => 5, 'name' => 'Samuel', 'created_date' => '2018-08-03']        
    ]);
    
    $filterd = $collection->sortBy(function ($data, $key) {
        return $data['name'].$data['email'];
    });

    $filterd->all();

    print_r($filterd);
}

Collection Sort By Count in Laravel

public function index()
{
    $collection = collect([
        ['id' => 1, 'name' => 'John', 'products' => ['shoes', 'belt', 'tshirt']],
        ['id' => 2, 'name' => 'Anna', 'products' => ['socks', 'handbag']],
        ['id' => 3, 'name' => 'Ricky', 'products' => ['jeans', 'sweater']],
        ['id' => 4, 'name' => 'Sam', 'products' => ['sweater', 'jacket']]
    ]);
    
    $filterd = $collection->sortBy(function ($data, $key) {
        return count($data['products']);
    });

    $filterd->all();

    print_r($filterd);
}

Collection Sort By Relation in Laravel

<?php
   
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Student;
  
class ProductController extends Controller
{
    
    public function index()
    {
        $student = Student::get()->sortBy(function($query){
            return $query->subject->name;
        })
        ->all();

        dd($student);
    }

}

Eventually, we have completed the Laravel SortBy tutorial, and i hope you have liked all the examples that we mention in this tutorial.