I needed to use the orderByRaw method as it seems to be the way to put null values last : order by -column desc (notice the '-').
For that purpose, I used a integer column named 'order', which works beautifully with orderBy('order', 'desc').
But when I moved to the orderByRaw('-order desc') way, I realized it no longer wraps the column names ("of course it's raw!"):
MySql was lost 'order by order', uh oh...
I looked for a clean way to do it, I mean I tried to find the way Laravel does it with the orderBy and others methods.
I came up to find the DB::getQueryGrammar()->wrap() method, so my builder now looks like this:
orderBy('-? ?', [DB::getQueryGrammar()->wrap('order'), 'desc'])
Am I wrong ? If not, maybe it could be a good thing to document it, and maybe add an easier shortcut like DB::wrap($column) (and maybe wrapTable?). I can PR if you would.
Hey Laravel Team,
I needed to use the
orderByRaw
method as it seems to be the way to put null values last :order by -column desc
(notice the '-'). For that purpose, I used a integer column named 'order', which works beautifully withorderBy('order', 'desc')
.But when I moved to the
orderByRaw('-order desc')
way, I realized it no longer wraps the column names ("of course it's raw!"): MySql was lost 'order by order', uh oh...I looked for a clean way to do it, I mean I tried to find the way Laravel does it with the orderBy and others methods. I came up to find the
DB::getQueryGrammar()->wrap()
method, so my builder now looks like this:orderBy('-? ?', [DB::getQueryGrammar()->wrap('order'), 'desc'])
Am I wrong ? If not, maybe it could be a good thing to document it, and maybe add an easier shortcut like
DB::wrap($column)
(and maybe wrapTable?). I can PR if you would.