[12.x] Add orderByWithPriority method to the query builder #57401
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces a new
orderByWithPrioritymethod for the query builder, allowing developers to sort results by a defined list of prioritized values — complementing existing methods likeorderBy,orderByRaw, andorderByDesc.Problem
While Eloquent provides expressive methods for sorting columns (
orderBy,orderByDesc, etc.), it currently lacks a concise way to order results based on custom priority lists.For example, suppose we want to list users so that admins appear first, followed by managers, then regular users. Today, this requires verbose SQL or raw expressions:
This approach is less readable, harder to maintain, and inconsistent with Laravel’s fluent query syntax.
Solution
The new
orderByWithPrioritymethod provides a clean, expressive way to handle this use case:This generates the equivalent SQL:
You can also combine it with other orderings:
Enum Support
The method supports PHP enums natively — it will automatically extract backing values:
This yields the same output, ensuring a seamless experience when using modern enum-backed domain models.
Before / After
Before
After
Behavior Matrix
['high', 'medium', 'low']FIELD(column, ...)[Status::Active, Status::Pending][]orderBy(column)['admin', Role::User]Naming
I considered alternatives like
orderByField,orderByValues, andorderByPriority, but settled onorderByWithPriorityfor its clarity and alignment with Laravel’s naming conventions (whereIn,withCount,withSum, etc.).It communicates both intent and flexibility — “order by this column, with this specific priority.”
Example Use Cases
Implementation Summary
Internally, the method wraps the column name with the query grammar and uses
orderByRawwith positional bindings to ensure safety and consistency. It automatically normalizes enums to their backing values.Tests
Added tests in
DatabaseQueryBuilderTestto ensure:Correct SQL and bindings are generated
Empty priorities gracefully fall back to standard ordering
Enum-backed priorities resolve correctly
Summary
Adds a fluent, expressive method for priority-based sorting
Reduces reliance on raw SQL
Supports modern PHP enums
Aligns perfectly with Laravel’s elegant query syntax