Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.