correctly sort by numeric columns

This commit is contained in:
2018-08-11 23:34:48 +02:00
parent 8d6ec7b47d
commit e1c7114bd4
6 changed files with 73 additions and 35 deletions
+22
View File
@@ -2,6 +2,9 @@
namespace App\Models;
use App\Tables\Column;
use Illuminate\Database\Eloquent\Builder;
/**
* Row in a data table
*
@@ -25,6 +28,25 @@ class Row extends BaseModel
$this->data->_id = $value;
}
public function scopeSortByCol(Builder $query, Column $column, $dir="asc")
{
$q = "data->>'".$column->id."'";
if ($column->type == 'int' || $column->type == 'float') {
$q = "CAST($q AS INTEGER)";
}
else if ($column->type == 'float') {
$q = "CAST($q AS FLOAT)";
}
if ($dir == 'asc') {
$q .= ' ASC';
} else {
$q .= ' DESC';
}
return $query->orderByRaw($q);
}
/**
* Allocate a single Global Row ID, application-unique.
*