77 lines
1.5 KiB
PHP
77 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire\Traits;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
trait WithSort {
|
|
|
|
/**
|
|
* Sort column
|
|
*/
|
|
public string $sort = '';
|
|
|
|
/**
|
|
* Sort direction
|
|
*/
|
|
public string $dir = 'asc';
|
|
|
|
/**
|
|
* Sort by column.
|
|
*/
|
|
public function sort($column)
|
|
{
|
|
if (!array_key_exists($column, $this->sort_columns)) {
|
|
return;
|
|
}
|
|
|
|
if ($this->sort === $column) {
|
|
$this->dir = $this->dir === 'asc' ? 'desc' : 'asc';
|
|
} else {
|
|
$this->sort = $column;
|
|
$this->reset('dir');
|
|
}
|
|
}
|
|
|
|
protected function sortApply($query)
|
|
{
|
|
// No sorting. bail out.
|
|
if (!strlen($this->sort)) {
|
|
return;
|
|
}
|
|
|
|
$columns = $this->sort_columns[$this->sort];
|
|
|
|
if (!is_array($columns)) {
|
|
$columns = [ $columns ];
|
|
}
|
|
|
|
foreach($columns as $column) {
|
|
|
|
// If column is a relationship
|
|
$p = strrpos($column, '.');
|
|
if ($p !== false) {
|
|
$rel = substr($column, 0, $p);
|
|
$col = substr($column, $p + 1);
|
|
try {
|
|
$query->orderByLeftPowerJoins([$rel, $this->orderExpr($col)], $this->dir);
|
|
} catch(\BadMethodCallException $e) {
|
|
$class = get_class($query->getModel());
|
|
throw new \Error("Failed to sort column '$column': '$class' "
|
|
. "must use 'Kirschbaum\PowerJoins\PowerJoins' "
|
|
. "trait to be able to sort by relationship.");
|
|
}
|
|
} else {
|
|
$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`");
|
|
}
|
|
}
|