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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-29 16:04:37 +0000
Seen: 12 times
Last updated: May 29 '23
How can a DateTime Zone be changed to another DateTime Zone?
How can we direct to a randomly selected entry from a list using Nginx?
How can I load a text file in gensim?
How to stop the for-loop when a specific condition is fulfilled?
How can CSS variables in Angular Material 15 be replaced?
How can you obtain several parameters using a single GET request?
How can double quotes be added to strings within an array?
"How can a Boolean variable be saved in SQFLite?"