sort_columns)) { 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_columns[$this->sort]; if (!is_array($columns)) { $columns = [ $columns ]; } foreach($columns as $column) { // 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); } } } /** * Bit of a hack to get nulls to appear last. */ private function orderExpr(string $col) : \Illuminate\Database\Query\Expression { return DB::raw("ISNULL(`$col`), `$col`"); } }