Home Laravel Laravel Orderby Relationship Column Example

Laravel Orderby Relationship Column Example

by Shahriar Sagor
Laravel Orderby Relationship

Hello Dev’s,
Today in this article, I will teach you record display in orderby on belongsto relationship. On orderBy function by use in relationship get record in ascending or descending order on out.Here we know Laravel Orderby Relationship.

So we can use with function on relationship through by get orderby record.

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

Solution 1 :

$users = User::with(['userCity' => function ($q){
	$q->orderBy('name');
}])->get();

Now in this solution sortBy method through get record in ascending order now.

Solution 2 :

$users = User::get()->sortBy(function($query){
	return $query->userCity->name;
});

Read Also: Upload image using ckeditor in Laravel

Thanks for read. I hope it help you. For more you can follow us on facebook.

close

You may also like