Archived
1
0
Fork 0

app/Http/Livewire/Traits/WithSort.php: sort relationships using PowerJoins

This commit is contained in:
Henrik Hautakoski 2022-01-15 13:31:10 +01:00
parent 2e64a9713b
commit ec4b04b40e

View file

@ -47,7 +47,23 @@ trait WithSort {
}
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);
}
}
}