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
A workaround for PostgreSQL
For numeric types:
DB::table('t')
->select(['id', 'val'])
->orderBy(DB::raw("coalesce(val, 0)"), 'desc')
For text types:
orderBy(DB::raw("coalesce(val, '')"), 'desc')
The trick is to replace NULL
values in the sorting column to zero (or empty string) so that it could be sorted as an ordinary integer (or text) value.