Ask Your Question
4

How can the sorting of relations be achieved using Laravel's YajraBox?

asked 2023-05-29 16:04:37 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-29 16:26:02 +0000

lakamha gravatar image

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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2023-05-29 16:04:37 +0000

Seen: 12 times

Last updated: May 29 '23