Skip to content

Conversation

ghabriel25
Copy link
Contributor

Just a shortcut of reorder() clause

@ghabriel25
Copy link
Contributor Author

ghabriel25 commented May 29, 2025

Add flexibility to reorder query based on a condition

use App\Models\Destination;
use App\Models\Flight;
use Illuminate\Support\Facades\Auth;

return Destination::orderBy('name')
    ->when(Auth::user() ?? false, 
        fn ($query, $user) =>$query->reorderDesc(
            Flight::select('arrived_at')
                ->whereColumn('destination_id', 'destinations.id')
                ->where('users.flight_id', $user->flight_id)
                ->orderByDesc('arrived_at')
                ->limit(1)
        )
    )->get();

@taylorotwell taylorotwell merged commit d3e8791 into laravel:12.x May 29, 2025
59 checks passed
@ghabriel25 ghabriel25 deleted the builder branch May 29, 2025 15:13
@ghabriel25 ghabriel25 changed the title Add reorderByDesc() to Query Builder Add reorderDesc() to Query Builder May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants