app/Http/Livewire/Traits/WithSort.php: Make null values appear last.
This commit is contained in:
parent
95751d0d9a
commit
2e64a9713b
1 changed files with 11 additions and 1 deletions
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
namespace App\Http\Livewire\Traits;
|
namespace App\Http\Livewire\Traits;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
trait WithSort {
|
trait WithSort {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -45,7 +47,15 @@ trait WithSort {
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($columns as $column) {
|
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`");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in a new issue