diff --git a/app/Http/Livewire/Traits/WithSort.php b/app/Http/Livewire/Traits/WithSort.php new file mode 100644 index 0000000..704b66b --- /dev/null +++ b/app/Http/Livewire/Traits/WithSort.php @@ -0,0 +1,51 @@ +sort_map)) { + 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_map[$this->sort]; + + if (!is_array($columns)) { + $columns = [ $columns ]; + } + + foreach($columns as $column) { + $query->orderBy($column, $this->dir); + } + } +}