Skip to content

Commit

Permalink
Added Possibility to define model for sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSantiago82 committed Apr 4, 2022
1 parent cbbac9f commit 4c3da64
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Http/Livewire/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,15 @@ protected function buildQuery()

if (Str::contains($this->sortBy, '.') === false) {
$query->orderBy($this->sortBy, $this->sortDirection);
} elseif($this->Data[$this->sortBy]['model'] !== null) {
$baseTableName = $query->newModelInstance()->getTable();
$relatedModelName = Str::before($this->sortBy, '.');
$relatedSearchField = Str::afterLast($this->sortBy, '.');
$relatedModel = '\\App\\Models\\'.$this->Data[$this->sortBy]['model'];
$relatedTableName = (new $relatedModel)->getTable();
$query->select($baseTableName.'.*');
$query->joinRelation($relatedModelName);
$query->orderBy($relatedTableName . '.'.$relatedSearchField, $this->sortDirection);
} else {
$baseTableName = $query->newModelInstance()->getTable();
$relatedModelName = Str::before($this->sortBy, '.');
Expand Down

0 comments on commit 4c3da64

Please sign in to comment.