laravel-cross-eloquent-search icon indicating copy to clipboard operation
laravel-cross-eloquent-search copied to clipboard

Sort results by nested relationship

Open spartyboy opened this issue 4 years ago • 2 comments

I want to sort main result set by relation in an order.. eg lets say a post has many tags i want to get all post but they should be sorted by their tags using eager load.

spartyboy avatar Dec 15 '20 11:12 spartyboy

Something likes this? (from the Laravel docs)

$orderBySubquery = Flight::select('arrived_at')
    ->whereColumn('destination_id', 'destinations.id')
    ->orderBy('arrived_at', 'desc')
    ->limit(1);

Search::add(Destination::class, 'location', $orderBySubquery)->get('Amsterdam');

pascalbaljet avatar Dec 17 '20 09:12 pascalbaljet

Yea so all destinations result would be ordered by flight order by

spartyboy avatar Dec 19 '20 04:12 spartyboy

For future readers of this issue, I recommend looking at this package: https://github.com/kirschbaum-development/eloquent-power-joins

pascalbaljet avatar Dec 12 '22 09:12 pascalbaljet