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...
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial