Exemple #1
0
 /**
  * @param \Illuminate\Database\Query\Builder $query
  * @param array $sortParameters
  *
  * @return \Illuminate\Database\Query\Builder
  *
  * @throws ColumnSortableException
  */
 private function queryOrderBuilder($query, array $sortParameters)
 {
     $model = $this;
     list($column, $direction) = $this->parseSortParameters($sortParameters);
     if (is_null($column)) {
         return $query;
     }
     if (method_exists($this, camel_case($column) . 'Sortable')) {
         return call_user_func_array([$this, camel_case($column) . 'Sortable'], [$query, $direction]);
     }
     $explodeResult = SortableLink::explodeSortParameter($column);
     if (!empty($explodeResult)) {
         $relationName = $explodeResult[0];
         $column = $explodeResult[1];
         try {
             $relation = $query->getRelation($relationName);
             $query = $this->queryJoinBuilder($query, $relation);
         } catch (BadMethodCallException $e) {
             throw new ColumnSortableException($relationName, 1, $e);
         } catch (\Exception $e) {
             throw new ColumnSortableException($relationName, 2, $e);
         }
         $model = $relation->getRelated();
     }
     if (isset($model->sortableAs) && in_array($column, $model->sortableAs)) {
         $query = $query->orderBy($column, $direction);
     } elseif ($this->columnExists($model, $column)) {
         $column = $model->getTable() . '.' . $column;
         $query = $query->orderBy($column, $direction);
     }
     return $query;
 }
 /**
  * @expectedException  Kyslik\ColumnSortable\Exceptions\ColumnSortableException
  * @expectedExceptionCode 0
  */
 public function testGetOneToOneSortThrowsException()
 {
     $sortParameter = 'relation-name..column';
     SortableLink::explodeSortParameter($sortParameter);
 }