AbstractQueryBuilder::orderBy

Add ORDER BY Sql statement portions into Query Builder.

@param string $field
@param string $direction

@return static
343059
public function orderBy($field, $direction = 'ASC')
    {
        $this->builderCache->store('orderBy', [$field => strtoupper($direction)]);

        return $this;
    }
1294824
public function orderBy($orderBy, $direction='asc')
    {
        if($direction == 'desc')
            return $this->model->orderBy($orderBy, 'desc');
        else
            return $this->model->orderBy($orderBy, 'asc');
    }
111503
public function OrderBy($Fields, $Direction = 'asc') {
      if (!$Fields)
         return $this;
         
      if ($Direction && $Direction != 'asc')
         $Direction = 'desc';
      else
         $Direction = 'asc';

      $this->_OrderBys[] = $this->EscapeIdentifier($Fields, TRUE).' '.$Direction;
      return $this;
   }
1220238
protected function buildOrder($query, $filter, $order, $direction)
    {
        if (!empty($order)) {
            $direction = empty($direction) ? 'asc' : $direction;
            $query     = $query->orderBy($order, $direction);
        }

        return $query;
    }
1325794
public function orderBy($column, $direction = 'asc')
    {
        return $this->order($this->mapper->getFilterPropertyName($column), $direction);
    }
973724
final public function setListOrderBy($fields, $direction = "ASC")
    {

        $direction = (in_array(strtoupper(trim($direction)), array('ASC', 'DESC'), TRUE)) ? ' ' . $direction : ' ASC';
        $orderby = NULL;
        //Clean up the order by field list
        if (!empty($fields) && !is_array($fields)) {
            $temp = array();
            foreach (explode(',', $fields) as $part) {
                $part = trim($part);
                $temp[] = $part;
            }

            $orderby = implode(', ', $temp);
        } else if (is_array($fields)) {
            $temp = array();
            foreach ($fields as $field) {
                $part = trim($field);
                $temp[] = $part;
            }
            $orderby = implode(', ', $temp);
        }


        if (!empty($orderby)) {
            $this->listOrderByStatement = "\nORDER BY " . $orderby . $direction;
        }
        //Return this object
        return $this;
    }
1360658
public function orderBy($key, $direction = self::ORDER_ASC, $tableName = null)
    {
        if (!$tableName) {
            $tableName = $this->getTableName();
        }
        $this->orderBy[] = ['key' => $key, 'direction' => $direction, 'tableName' => $tableName];
        return $this;
    }
75621
public function orderBy($field, $direction = 1)
    {
        if (is_string($direction)) {
            $direction = $direction === 'asc' ? 1 : -1;
        }

        $this->orderBy[$field] = $direction;

        return $this;
    }
42929
public function orderBy($column, $direction = 'asc')
    {
        $property = $this->unions ? 'unionOrders' : 'orders';

        $direction = strtolower($direction) == 'asc' ? 'asc' : 'desc';

        $this->{$property}[] = compact('column', 'direction');

        return $this;
    }
1311603
public function orderBy($field, $direction = self::DESC) {
        if (is_array($field)) {
            foreach ($field as $key => $dir) {
                $this->orderBy($key, $dir);
            }

        } else if ($field === 'RAND') {
            $this->_orderBy[] = $this->func('RAND');

        } else if ($field instanceof Func) {
            $this->_orderBy[] = $field;

        } else {
            $direction = strtolower($direction);

            if ($direction != self::ASC && $direction != self::DESC) {
                throw new InvalidArgumentException(sprintf('Invalid order direction %s for field %s', $direction, $field));
            }

            $this->_orderBy[$field] = $direction;
        }

        return $this;
    }
863379
public function scopeOrderByRating(Builder $query, $direction = 'desc'): Builder
    {
        $rateable = $query->getModel();

        return $query
            ->select($rateable->getTable() . '.*', 'ratings_total.rating')
            ->leftJoin('ratings_total', function (JoinClause $join) use ($rateable) {
                $join
                    ->on('ratings_total.rateable_id', '=', "{$rateable->getTable()}.{$rateable->getKeyName()}")
                    ->where('ratings_total.rateable_type', '=', $rateable->getMorphClass());
            })
            ->orderBy('ratings_total.rating', $direction);
    }
512277
public function orderBy($column, $direction = IQueryBuilder::ORDER_ASC)
    {
        if ($direction == IQueryBuilder::ORDER_ASC || $direction == IQueryBuilder::ORDER_DESC) {
            $this->orderConditions[$column] = $direction;
        }

        return $this;
    }
428041
public function apply($query, $column, $direction = 'asc')
    {
        $direction = $direction === 'desc' ? 'desc' : 'asc';

        if ($query instanceof Model) {
            $query = $query->query();
        }

        $query = $this->applyNullLastQuery($query, $column);

        return $query->orderBy($column, $direction);
    }
778586
public function order($columns, $direction = 'ASC')
    {
        foreach ((array) $columns as $column)
        {
            $this->order[] = array(
                'column'    => $column,
                'direction' => $direction
            );
        }

        return $this;
    }
1336908
public function addOrderByField($field = 'order', $direction_desc = false, $table = '', $do_not_use_table_in_sql = false): self
    {
        // No table provided
        if (!$table) {
            $table = $this->getDbTableName();
        }

        $direction = $direction_desc ? 'DESC' : ' ASC';

        // Swap fields with translation table
        if (\in_array($field, $this->translation_fields, true)) {
            $this->translation_join_count++;
            $this->addJoinTable(['cms_translations', $this->getTranslationTableJoinAlias() . $this->translation_join_count], 'id', $field, 'LEFT', $table);

            $this->order_fields[] = [
                'table'                   => false,
                'field'                   => '`' . $this->getTranslationTableJoinAlias() . $this->translation_join_count . '`.`' . $this->getLanguage() . '`',
                'direction'               => $direction,
                'do_not_use_table_in_sql' => true,
                'type'                    => 'string'
            ];
        } else {
            $this->order_fields[] = [
                'table'                   => $table,
                'field'                   => $field,
                'direction'               => $direction,
                'do_not_use_table_in_sql' => $do_not_use_table_in_sql,
                'type'                    => 'simple'
            ];
        }

        return $this;
    }
1306093
public function scopeOrderByViews(Builder $query, string $direction = 'desc', $period = null): Builder
    {
        return $query->withCount(['views' => function ($query) use ($period) {
            if ($period) {
                $query->withinPeriod($period);
            }
        }])->orderBy('views_count', $direction);
    }
1233565
private function eventQuery(string $direction)
    {
        return DB::table('dates')
            ->select(['d_gid as id', 'd_year as year', 'd_fact AS fact', 'd_type AS type'])
            ->where('d_file', '=', $this->tree->id())
            ->where('d_gid', '<>', 'HEAD')
            ->whereIn('d_fact', $this->getCommonFacts())
            ->where('d_julianday1', '<>', 0)
            ->orderBy('d_julianday1', $direction)
            ->orderBy('d_type')
            ->first();
    }