Skip to content

Add reorderDesc() to Query Builder #55885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 29, 2025
Merged

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