Laravel Eloquent Collection Sort By Relation column

Today, We want to share with you Laravel Eloquent Collection Sort By Relation column.In this post we will show you Laravel Order By Relationship Sum Column Example, hear for How to order by has many relationship column value we will give you demo and example for implement.In this post, we will learn about laravel collection->sort alphabetically with an example.

Laravel Eloquent Collection Sort By Relation column

There are the Following The simple About laravel collection->sort by date desc Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel orderby belongsto relationship, 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 Relation

Sort collection by relationship value Example

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

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 Sorting Nested Laravel Collections (Example).
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.

Leave a Comment