Laravel Orderby Relationship Column Example




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){



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 6