protected function checkQueryGroupBy() { $groups = $this->query->getQuery()->groups; $keyGroup = $this->model->getQualifiedKeyName(); if (empty($groups) || !in_array($keyGroup, $groups)) { $this->query->groupBy($keyGroup); } }
/** * EloquentManager constructor. * * @param Builder|\Eloquent $model * @param Request $request */ public function __construct($model, Request $request) { $this->response = new DataTable(); $this->query = $model instanceof Builder ? $model : $model->getQuery(); $this->request = $request; // $this->columns = $this->query->columns; $this->connection = $model->getConnection(); $this->prefix = $this->connection->getTablePrefix(); $this->database = $this->connection->getDriverName(); }
/** * Counts current query * * @param string $count variable to store to 'count_all' for iTotalRecords, 'display_all' for iTotalDisplayRecords * * @return null */ protected function count($count = 'count_all') { //Get columns to temp var. if ($this->query_type == 'eloquent') { $query = $this->query->getQuery(); $connection = $this->query->getModel()->getConnection()->getName(); } else { $query = $this->query; $connection = $query->getConnection()->getName(); } // if its a normal query ( no union ) replace the select with static text to improve performance $countQuery = clone $query; if (!preg_match('/UNION/i', $countQuery->toSql())) { $countQuery->select(DB::raw("'1' as row")); // if query has "having" clause add select columns if ($countQuery->havings) { foreach ($countQuery->havings as $having) { if (isset($having['column'])) { $countQuery->addSelect($having['column']); } else { // search filter_columns for query string to get column name from an array key $found = false; foreach ($this->filter_columns as $column => $filter) { if ($filter['parameters'][0] == $having['sql']) { $found = $column; break; } } // then correct it if it's an alias and add to columns if ($found !== false) { foreach ($this->columns as $col) { $arr = preg_split('/ as /i', $col); if (isset($arr[1]) && $arr[1] == $found) { $found = $arr[0]; break; } } $countQuery->addSelect($found); } } } } } // Clear the orders, since they are not relevant for count $countQuery->orders = null; $this->{$count} = DB::connection($connection)->table(DB::raw('(' . $countQuery->toSql() . ') AS count_row_table'))->setBindings($countQuery->getBindings())->count(); }
/** * Return whether the method * exists on the query or not. * * @param $name * @return bool */ protected function methodExists($name) { return method_exists($this->query->getQuery(), $name); }
/** * Generic query used by scopes, but you can call it with custom feataure codes. * * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query * @param String $name * @param String $feature_class The 1 character feature class * @param array $featureCodes List of feature codes to use when returning results * @return \Illuminate\Database\Query\Builder */ public function scopeSearchByFeature($query, $name = null, $feature_class = null, $featureCodes = null) { $table = 'geonames_geonames'; if (!isset($query->getQuery()->columns)) { $query = $query->addSelect($this->usefulScopeColumns); } if ($name !== null) { $query = $query->where($table . '.name', 'LIKE', $name); } $query = $query->where($table . '.feature_class', $feature_class)->whereIn($table . '.feature_code', $featureCodes); return $query; }
/** * Remove the GROUP BY clause from a builder. * * @param Builder|QueryBuilder $builder * @return Builder|QueryBuilder $builder with the groups property set to null. */ private function removeGroupBy($builder) { // Handle \Illuminate\Database\Eloquent\Builder if ($builder instanceof Builder) { $query = $builder->getQuery(); $query->groups = null; $builder->setQuery($query); } else { $builder->groups = null; } return $builder; }