Laravel Orderby Relationship Column Example

Laravel 6 , Laravel

Nicesnippets

14606

12-11-2019


Laravel Orderby Relationship Column Example

Hi Dev,

In this solution, I will show you record display in orderby on belongsto relationship. orderBy function use in relationship get record in ascending or descending order.

you can use with function on relationship through get orderby record.

In this solution in orderBy function use to get record in ascending order

Solution 1 :

$users = User::with(['userCity' => function ($q){

$q->orderBy('name');

}])->get();

In this solution sortBy method through get record in ascending order.

Solution 2 :

$users = User::get()->sortBy(function($query){

return $query->userCity->name;

});

It will help you...


Recommended Posts