select() публичный метод

Set the columns to be selected.
public select ( array | mixed $columns = ['*'] )
$columns array | mixed
Пример #1
0
 /**
  * Add subselect queries to count the relations.
  *
  * @param  mixed  $relations
  * @return $this
  */
 public function withCount($relations)
 {
     if (is_null($this->query->columns)) {
         $this->query->select(['*']);
     }
     $relations = is_array($relations) ? $relations : func_get_args();
     foreach ($this->parseWithRelations($relations) as $name => $constraints) {
         // First we will determine if the name has been aliased using an "as" clause on the name
         // and if it has we will extract the actual relationship name and the desired name of
         // the resulting column. This allows multiple counts on the same relationship name.
         $segments = explode(' ', $name);
         if (count($segments) == 3 && Str::lower($segments[1]) == 'as') {
             list($name, $alias) = [$segments[0], $segments[2]];
         }
         $relation = $this->getHasRelationQuery($name);
         // Here we will get the relationship count query and prepare to add it to the main query
         // as a sub-select. First, we'll get the "has" query and use that to get the relation
         // count query. We will normalize the relation name then append _count as the name.
         $query = $relation->getRelationCountQuery($relation->getRelated()->newQuery(), $this);
         $query->callScope($constraints);
         $query->mergeModelDefinedRelationConstraints($relation->getQuery());
         // Finally we will add the proper result column alias to the query and run the subselect
         // statement against the query builder. Then we will return the builder instance back
         // to the developer for further constraint chaining that needs to take place on it.
         $column = snake_case(isset($alias) ? $alias : $name) . '_count';
         $this->selectSub($query->toBase(), $column);
     }
     return $this;
 }
 /**
  * Build the search subquery.
  *
  * @param  array  $words
  * @param  array  $columns
  * @param  string $groupBy
  * @param  float  $threshold
  * @return \Sofa\Searchable\Subquery
  */
 protected function buildSubquery(array $words, array $columns, $groupBy, $threshold)
 {
     $columns = $this->columns($columns);
     if (is_null($threshold)) {
         $this->threshold = array_reduce($columns, function ($sum, $column) {
             return $sum + $column->getWeight() / 4;
         });
     } else {
         $this->threshold = (double) $threshold;
     }
     if (strpos($groupBy, '.') === false) {
         $groupBy = $this->query->from . '.' . $groupBy;
     }
     $this->query->select($this->query->from . '.*')->groupBy($groupBy);
     $this->addSearchClauses($columns, $words, $this->threshold);
     return $this;
 }
 /**
  * Add subselect queries to count the relations.
  *
  * @param  mixed  $relations
  * @return $this
  */
 public function withCount($relations)
 {
     if (is_null($this->query->columns)) {
         $this->query->select(['*']);
     }
     $relations = is_array($relations) ? $relations : func_get_args();
     foreach ($this->parseWithRelations($relations) as $name => $constraints) {
         // Here we will get the relationship count query and prepare to add it to the main query
         // as a sub-select. First, we'll get the "has" query and use that to get the relation
         // count query. We will normalize the relation name then append _count as the name.
         $relation = $this->getHasRelationQuery($name);
         $query = $relation->getRelationCountQuery($relation->getRelated()->newQuery(), $this);
         call_user_func($constraints, $query);
         $this->selectSub($query->getQuery(), snake_case($name) . '_count');
     }
     return $this;
 }
Пример #4
0
 private function buildQuery()
 {
     $this->query = app(get_class($this->model));
     if (!empty($this->fields)) {
         $this->query = $this->query->select($this->fields);
     }
     if (!empty($this->relations)) {
         $this->relations = array_unique($this->relations);
         $this->query = $this->query->with($this->relations);
     }
     if (!empty($this->per_page)) {
         $this->query = $this->query->take($this->per_page);
     }
     if (count($this->conditions)) {
         foreach ($this->conditions as $condition) {
             $this->query = $this->query->where($condition['column'], $condition['operator'], $condition['value'], $condition['boolean']);
         }
     }
 }
Пример #5
0
 /**
  * @param Builder $query
  * @param int $userId
  * @param int $parentId
  */
 public function scopeGetByUserId($query, $userId, $parentId = 0)
 {
     $minStatus = DB::table('messages_users as ms')->select(DB::raw('MIN(ms.status)'))->where('ms.parent_id', DB::raw('messages_users.message_id'))->orWhere('ms.message_id', DB::raw('messages_users.message_id'))->toSql();
     $query->select($this->getTable() . '.*', 'users.username as author', 'messages_users.status', DB::raw("({$minStatus}) as is_read"))->leftJoin('messages_users', $this->getTable() . '.id', '=', 'messages_users.message_id')->leftJoin('users', $this->getTable() . '.from_user_id', '=', 'users.id')->where('messages_users.user_id', (int) $userId)->where('messages_users.parent_id', (int) $parentId)->orderBy($this->getTable() . '.created_at', 'desc')->get();
 }
Пример #6
0
 /**
  * Select users which have the required permission
  * @param \Illuminate\Database\Query\Builder $query
  * @param string $permission Permission to check
  */
 public function scopeWithPermission($query, $permission)
 {
     $query->whereExists(function ($query) use($permission) {
         $query->select(DB::Raw('1'))->from('permission_role')->whereRaw('permission_role.role_id = users.role_id')->where('permission_role.permission_id', '=', function ($query) use($permission) {
             $query->select('id')->from('permissions')->where('ime', '=', $permission);
         });
     });
 }
Пример #7
0
 /**
  * Add selects to query builder
  * @param \Illuminate\Database\Query\Builder $query object is by reference
  */
 protected function addSelectQuery($query)
 {
     // Convert id into clients.id as id
     // Convert host.serverNum into hosts.server_num as host.serverNum ...
     // This one is odd becuase if column is in this entity, use COLUMN names
     // not property names because we have to transformStore next which requires column style.
     // BUT if column is subentity, then use entity names not colum names because we have already transformed the subentity
     // ex:
     /*array:9 [▼
         0 => "id"
         1 => "name"
         2 => "addressID"
         3 => "host.key"
         4 => "host.serverNum"
         5 => "address.address"
         6 => "address.city"
         7 => "address.stateKey"
         8 => "disabled"
       ]*/
     // Translates to
     /*
             array:9 [▼
      0 => "clients.id as id"
      1 => "clients.name as name"
      2 => "clients.address_id as address_id"
      3 => "hosts.key as host.key"
      4 => "hosts.server_num as host.serverNum"
      5 => "addresses.address as address.address"
      6 => "addresses.city as address.city"
      7 => "addresses.state as address.stateKey"
      8 => "clients.disabled as disabled"
             ]
     */
     $selects = [];
     if (isset($this->select)) {
         foreach ($this->select as $select) {
             $mappedSelect = $this->map($select, true);
             // host.serverNum
             list($table, $item) = explode('.', $select);
             if (str_contains($mappedSelect, '.')) {
                 list($table, $item) = explode('.', $mappedSelect);
                 $selects[] = "{$select} as {$table}.{$item}";
             } else {
                 $selects[] = "{$select} as {$item}";
             }
         }
     }
     // Add withCount (count column)
     if ($this->withCount) {
         $selects[] = DB::raw('count(*) as count');
     }
     $query->select($selects ?: ['*']);
     // Add distinct
     if ($this->distinct) {
         $query->distinct();
     }
 }
Пример #8
0
 /**
  * @param \Illuminate\Database\Query\Builder $query
  * @param  $relation
  *
  * @return \Illuminate\Database\Query\Builder
  *
  * @throws \Exception
  */
 private function queryJoinBuilder($query, $relation)
 {
     $relatedModel = $relation->getRelated();
     $relatedTable = $relatedModel->getTable();
     $parentModel = $relation->getParent();
     $parentTable = $parentModel->getTable();
     if ($relation instanceof HasOne) {
         $relatedPrimaryKey = $relation->getForeignKey();
         $parentPrimaryKey = $parentTable . '.' . $parentModel->primaryKey;
         return $query->select($parentTable . '.*')->join($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey);
     } elseif ($relation instanceof BelongsTo) {
         $relatedPrimaryKey = $relatedTable . '.' . $relatedModel->primaryKey;
         $parentPrimaryKey = $parentTable . '.' . $relation->getForeignKey();
         return $query->select($parentTable . '.*')->join($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey);
     } else {
         throw new \Exception();
     }
 }
Пример #9
0
 /**
  * Set the columns to be selected.
  *
  * @param  array|mixed  $columns
  * @return $this
  */
 public function select($columns = ['*'])
 {
     parent::select($columns);
     $this->columns = $this->qualifyColumns($this->columns);
     return $this;
 }
Пример #10
0
 /**
  * Lấy $take ebooks phục vụ selectize ebooks
  *
  * @param \Illuminate\Database\Query\Builder|static $query
  * @param int $take
  *
  * @return \Illuminate\Database\Query\Builder|static
  */
 public function scopeForSelectize($query, $take = 50)
 {
     return $query->select(['id', 'title'])->take($take);
 }
Пример #11
0
 /**
  * @param \Illuminate\Database\Query\Builder|static $query
  *
  * @return \Illuminate\Database\Query\Builder|static
  */
 public function scopeQueryDefault($query)
 {
     return $query->select("{$this->table}.*");
 }
Пример #12
0
 /**
  * 
  * @param \Illuminate\Database\Query\Builder $query
  * @param int $user_id
  * @return \Illuminate\Database\Query\Builder
  */
 public function scopeWithUser($query, $user_id)
 {
     return $query->whereExists(function ($query) use($user_id) {
         $query->select(DB::Raw('1'))->from('predmet_user')->whereRaw('predmet_user.predmet_id = predmeti.id')->where('predmet_user.user_id', '=', $user_id);
     });
 }
 /**
  * @param Builder|QueryBuilder $queryBuilder
  * @param array $columns
  */
 protected function buildFields($queryBuilder, $columns = ['*'])
 {
     $queryBuilder->select($columns);
 }
Пример #14
0
 /**
  * Attaches our column select statements to the query. Unfortunately, this
  * can't be done from listExtendQueryBefore. Select statements that are
  * added before processing the query will be removed by the behavior.
  *
  * @param  \Illuminate\Database\Query\Builder $query
  * @return \Illuminate\Database\Query\Builder
  */
 public function listExtendQuery($query)
 {
     $query->select('bedard_shop_products.*', 'inventory', 'price')->with(['current_price.discount' => function ($discount) {
         $discount->select('id', 'name', 'end_at');
     }])->selectStatus();
 }
Пример #15
0
 /**
  * Return the paginated result of the query
  *
  * @param   Builder $results    The query builder that contains all the clauses that have been applied
  * @param   array $filters      The filters that are to be applied to the query. The key identifies the database column, the value in the array is the value that is to be matched in the query
  * @param   int $size           The amount of results that we expect to receive from the paginator
  * @return mixed
  */
 protected function paginated($results, $filters = array(), $size = 25)
 {
     return $results->select($this->getTable() . '.*')->paginate($size)->appends($filters)->appends('size', $size);
 }
Пример #16
0
 /**
  * Set the columns to be selected.
  *
  * @param array|mixed $columns
  * @return $this 
  * @static 
  */
 public static function select($columns = array())
 {
     return \Illuminate\Database\Query\Builder::select($columns);
 }
 /**
  * @param EloquentBuilder|QueryBuilder|Model $query
  * @param string $key
  * @param bool $returnExpression
  *
  * @return string
  */
 public function scopeToSubQuery($query, $key, $returnExpression = false)
 {
     $index = 0;
     $bindings = array();
     if (!Str::contains($key, '.')) {
         /** @var Model|BetterEloquentTrait $model */
         $model = $query->getModel();
         $key = $model->getField($key);
     }
     $sql = $query->select(array($key))->toSql();
     foreach ($query->getBindings() as $binding) {
         $bindings[] = is_array($binding) ? array_merge($bindings, $binding) : $binding;
     }
     while (Str::contains($sql, '?')) {
         $sql = $this->replaceFirst('?', $bindings[$index++], $sql);
     }
     $sql = "({$sql})";
     return $returnExpression ? new Expression($sql) : $sql;
 }