1

I'm using Laravel 4.2 and have a query builder object. The column I am trying to order by is nullable. When the value is null it treats it as the lowest value. I need to make it treat null as the highest value.

public function sort($builder, $direction = 'asc')
{
    return $builder->orderBy('table.nullable_column', $direction);
}

Honestly I just need the null values to be last when sorting ascending and last when descending. Open to suggestions if Laravel doesn't have a way to handle this. Was unable to find anything else using google. Hopefully you guys can help.

Thanks!

Dylan Buth
  • 1,648
  • 5
  • 35
  • 57

1 Answers1

2

Simple put a - sign before the column

public function sort($builder, $direction = 'asc')
{
    return $builder->orderBy('-table.nullable_column', $direction);
}

reference: MySQL Orderby a number, Nulls last

Community
  • 1
  • 1
yangqi
  • 673
  • 6
  • 13