The sorting of relations in Laravel's YajraBox can be achieved by using the "with" method and passing a closure as the second parameter.
Example:
$tableData = User::with(['orders'=>function($q){
$q->orderBy('created_at', 'desc');
}])->get();
In the above example, we are fetching users with their orders sorted in descending order of creation date.
Similarly, you can use the orderBy method to sort the relations by any field using the same closure approach.
Example:
$tableData = User::with(['orders'=>function($q){
$q->orderBy('total_amount', 'asc');
}])->get();
In the above example, we are fetching users with their orders sorted in ascending order of total amount.
Asked: 2023-05-29 16:04:37 +0000
Seen: 13 times
Last updated: May 29 '23