Posted inLaravel

PHP Laravel Sorting collection SortBy

Today, We want to share with you PHP Laravel Sorting collection SortBy.In this post we will show you laravel sortby direction, hear for laravel collection->sort alphabetically we will give you demo and example for implement.In this post, we will learn about laravel collection->sort by date desc with an example.

PHP Laravel Sorting collection SortBy

There are the Following The simple About laravel collection chain sortby Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel add to collection, so the some major files and Directory structures for this example is following below.

Laravel Collection SortBy Tutorial with Examples

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 Simple Example

public function index()
{
    $collection = collect([
        ['id' => 1, 'name' => 'Hanuman', 'email' => '[email protected]'],
        ['id' => 2, 'name' => 'Abhishek', 'email' => '[email protected]'],
        ['id' => 3, 'name' => 'Bharat', 'email' => '[email protected]'],
    ]);
  
    $sorted = $collection->sortBy('name');
  
    $sorted->all();
    
    dd($sorted);
}

Here, You can More Information with Details for PHP Laravel Sorting Sort By Collection

Laravel Collection Sort By Count

PHP Laravel Sort a multidimensional array by number of items

public function index()
{
    $collection = collect([
            ['id' => 1, 'name' => 'Vishal', 'points' => ['v11', 'v85']],
            ['id' => 2, 'name' => 'Ashalo', 'points' => ['a23']],
            ['id' => 3, 'name' => 'Ajay', 'points' => ['s5', 's6', 's7']],
        ]);
  
    $sorted = $collection->sortBy(function ($product, $key) {
                        return count($product['points']);
                    });
  
    $sorted->all();
    
    dd($sorted);
}

Here, You can More Information with Details for Laravel Sort By List Of Collection Using Counting Sort

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);
}

Here, You can More Information with Details for Sort Collection By Dates In Laravel

Laravel Collection Sort By Two Fields

The Keys are the Key in Laravel Multiple Field sortBy() : PHP

public function index()
{
    $collection = collect([
            ['id' => 1, 'name' => 'Hanuman', 'city' => 'Mahshana'],
            ['id' => 2, 'name' => 'abhishek', 'city' => 'Rajesthan'],
            ['id' => 3, 'name' => 'Bharat', 'city' => 'Rajesthan'],
            ['id' => 4, 'name' => 'abhishek', 'city' => 'Mahshana'],
        ]);
  
    $sorted = $collection->sortBy(function ($product, $key) {
                        return $product['city'].$product['name'];
                    });
  
    $sorted->all();
  
    dd($sorted);
}

Here, You can More Information with Details for PHP Laravel Collection Sort By Multiple Fields

Laravel Collection Sort By Relation

Sort collection by relationship value Example

public function index()
{
    $students = Student::get()->sortBy(function($query){
                        return $query->teacher->name;
                    })
                    ->all();
}

Here, You can More Information with Details for Laravel Eloquent Collection Sort By Relation Column

Sort collection by relationship value

For exapmle:

Project::join('activities', 'activities.application_id', '=', 'applications.id')
        ->select('applications.*', DB::raw("MAX(activities.deadline) as deadline_date"))
        ->groupBy('activities.application_id')
        ->orderBy('deadline_date')
        ->get()
Project::join('activities', function ($join) {
            $join->on('activities.application_id', '=', 'applications.id')
                ->where('activities.user_id', Auth::user()->id)
                ->whereNull('activities.completed');
        })
        ->select('applications.*', DB::raw("MAX(activities.deadline) as deadline_date"))
        ->groupBy('activities.application_id')
        ->orderBy('deadline_date')
        ->get()
->with(['activities' => function ($q) {
    $q->where('user_id', Auth::user()->id)
       ->whereNull('completed');
})
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel collection->sortby multiple.
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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype