app/Http/Livewire/Traits/WithSort.php: sort relationships using PowerJoins
This commit is contained in:
parent
2e64a9713b
commit
ec4b04b40e
1 changed files with 17 additions and 1 deletions
|
|
@ -47,7 +47,23 @@ trait WithSort {
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach($columns as $column) {
|
foreach($columns as $column) {
|
||||||
$query->orderBy($this->orderExpr($column), $this->dir);
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Reference in a new issue