diff --git a/app/Http/Livewire/Traits/WithSort.php b/app/Http/Livewire/Traits/WithSort.php index f795fec..2303ed7 100644 --- a/app/Http/Livewire/Traits/WithSort.php +++ b/app/Http/Livewire/Traits/WithSort.php @@ -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); + } } }