From 2e64a9713b645f645774e1003dd64b38dd766a22 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sat, 15 Jan 2022 13:30:41 +0100 Subject: [PATCH] app/Http/Livewire/Traits/WithSort.php: Make null values appear last. --- app/Http/Livewire/Traits/WithSort.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/Http/Livewire/Traits/WithSort.php b/app/Http/Livewire/Traits/WithSort.php index 854531c..f795fec 100644 --- a/app/Http/Livewire/Traits/WithSort.php +++ b/app/Http/Livewire/Traits/WithSort.php @@ -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`"); + } }