Improve @orderBy
directive to allow developer to specify null behaviour
#2486
Labels
enhancement
A feature or improvement
What problem does this feature proposal attempt to solve?
Sometimes it's important to be able to specify whether nulls always come first or always come last.
As a developer I should be able to specify whether nulls are returned in their default position, always come first or always come last.
This option could be extended to the user as an additional argument on the order argument - if it is, then it should be optional.
Which possible solutions should be considered?
As far as I'm aware, Laravel doesn't have a solution for this, so it would require using raw queries (
orderByRaw
), and I don't know the best way to handle that to ensure support for all the databases Laravel support (which is currently MariaDB 10.10+, MySQL 5.7+, PostgreSQL 11.0+, SQLite 3.8.8+, SQL Server 2017+)Here's how to order by nulls first or nulls last in PostgreSQL and MySQL:
PostgreSQL
You can simply add
NULLS LAST
orNULLS FIRST
to the end of the raw queries.MySQL
Mysql doesn't have an explicit way to handle it, but I would just use IS NULL on my sorting column first, then add my sorting column as a secondary sort:
(Use
IS NULL ASC
for nulls last andIS NULL DESC
for nulls first)The text was updated successfully, but these errors were encountered: