I\'ve got a many to many relationship between my employees and groups table. I\'ve created the pivot table, and all is working correctly with that. However, I\'ve got a sort
Just add a minus sign to field and change order to DESC.
$q->orderBy(\DB::raw('-`sortOrder`'), 'desc');
I ran into this problem recently using Laravel 5.6, where junkystu answer was perfect for me. However our testing framework uses sqlite, so tests were constantly returning a 500 error.
This is what we came up with, which should be slightly more agnostic of a DB driver.
Ascending
$query->orderBy(DB::raw('column_to_sort IS NULL, column_to_sort'), 'asc');
Descending
$query->orderBy(DB::raw('column_to_sort IS NOT NULL, column_to_sort'), 'desc');