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.

Read Also:  How to Create, Access and Destroy Sessions in Laravel

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

Read Also:  Eloquent Order By Relation Column in Laravel

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 :

Read Also:  PHP Upload File Size Limit

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Create array in laravel 5
  2. Read Also:  Simple Laravel 5 Vue JS Ajax CRUD(insert update delete)
  3. Read Also:  Sort Collection By dates in Laravel
  4. Read Also:  How to Merge Two Eloquent Collections in Laravel?
  5. Read Also:  Laravel 5.7 Email Verification with Activation Tutorial Example
  6. Read Also:  How to Validate Date String in Laravel?
  7. Read Also:  Get Unique Collection values(Remove Duplicates from Collection) using Laravel
  8. Read Also:  Laravel Collection Contains Example
  9. Read Also:  PHP Laravel Sorting Sort By Collection
  10. Read Also:  global variable for all controller and views in Laravel
CLOSEX