Laravel Orderby Relationship Column Example

10-Apr-2023

.

Admin

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...

#Laravel

#Laravel 6