1
0
Fork 0

app/Http/Livewire/Traits/WithSort.php: Make null values appear last.

This commit is contained in:
Henrik Hautakoski 2022-01-15 13:30:41 +01:00
parent 95751d0d9a
commit 2e64a9713b

View file

@ -2,6 +2,8 @@
namespace App\Http\Livewire\Traits;
use Illuminate\Support\Facades\DB;
trait WithSort {
/**
@ -45,7 +47,15 @@ trait WithSort {
}
foreach($columns as $column) {
$query->orderBy($column, $this->dir);
$query->orderBy($this->orderExpr($column), $this->dir);
}
}
/**
* Bit of a hack to get nulls to appear last.
*/
private function orderExpr(string $col) : \Illuminate\Database\Query\Expression
{
return DB::raw("ISNULL(`$col`), `$col`");
}
}