public function build() { if (is_string($this->source) && strpos(" ", $this->source) === false) { //tablename $this->type = "query"; $this->query = $this->table($this->source); $this->total_rows = $this->query->count(); } elseif (is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Model") || is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Builder")) { $this->type = "model"; $this->query = $this->source; $this->total_rows = $this->query->count(); } elseif (is_array($this->source)) { $this->type = "array"; $this->total_rows = count($this->source); } //exception //offset and pagination setup/detect $config = array('cid' => $this->cid, 'total_items' => $this->total_rows, 'items_per_page' => $this->per_page, 'num_links' => round($this->num_links / 2), 'hash' => $this->hash, 'url' => $this->url, 'current_page' => $this->current_page); $this->pagination = new \Rapyd\Helpers\Pagination($config); $offset = $this->pagination->offset(); $this->limit($this->per_page, $offset); //build orderby urls $this->orderby_uri_asc = $this->app->url->remove('pag' . $this->cid)->remove('reset' . $this->cid)->append('orderby' . $this->cid, array("-field-", "asc"))->get() . $this->hash; $this->orderby_uri_desc = $this->app->url->remove('pag' . $this->cid)->remove('reset' . $this->cid)->append('orderby' . $this->cid, array("-field-", "desc"))->get() . $this->hash; //detect orderby $orderby = $this->app->url->value("orderby" . $this->cid); if ($orderby) { $this->orderby_field = $orderby[0]; $this->orderby_direction = $orderby[1]; $this->orderby($this->orderby_field, $this->orderby_direction); } //build subset of data switch ($this->type) { case "array": //orderby if (isset($this->orderby)) { list($field, $direction) = $this->orderby; $column = array(); foreach ($this->source as $key => $row) { $column[$key] = $row[$field]; } if ($direction == "asc") { array_multisort($column, SORT_ASC, $this->source); } else { array_multisort($column, SORT_DESC, $this->source); } } //limit-offset if (isset($this->limit)) { $this->source = array_slice($this->source, $this->limit[1], $this->limit[0]); } $this->data = $this->source; break; case "query": case "model": //orderby if (isset($this->orderby)) { $this->query = $this->query->orderBy($this->orderby[0], $this->orderby[1]); } //limit-offset if (isset($this->limit)) { $this->query = $this->query->skip($this->pagination->offset())->take($this->per_page); } $this->data = $this->query->get(); break; } return $this; }
/** * Check if the constraint is met. * * @param mixed $value * @param array $context * * @return mixed */ public function check($value, array $context = []) { if (count($this->where) > 0) { $query = clone $this->query; foreach ($this->where as $field => $value) { $query->where($field, '=', $context[$value]); } return $query->count() === 1; } return $this->query->count() === 1; }
public function totalCount() { if ($this->options['distinctCountGroup'] && count($this->originalBuilder->groups) == 1) { $this->originalBuilder->groups = null; } if ($this->options['searchWithAlias']) { $cnt = count($this->originalBuilder->get()); } else { $cnt = $this->originalBuilder->count(); } return $cnt; }
protected function paginate(Builder $builder, $perPage) { $page = Paginator::resolveCurrentPage(); $total = $builder->count(); $query = $builder->forPage($page, $perPage); $results = $query->get(); return new LengthAwarePaginator($results, $total, $perPage, $page, ['path' => Paginator::resolveCurrentPath()]); }
/** * Return the filtered, ordered and paginated rows from the crud's custom query * * @param Builder $query * @param int $start * @param int $length * @param array $filters * @param null $order * @return array */ public static function getFilteredQueryListRows(Builder $query, $start, $length, $filters = [], $order = null) { // Get the total count of rows with no filters and pagination $countTotal = $query->count(); // Check if any filters were submitted if ($filters) { foreach ($filters as $filterName => $filterValue) { // Check if there is any filter operator in the field string $operation = SQL::findOperationWhere($filterValue); $query->where($filterName, $operation['operation'], $operation['text']); } } // Get the total count of rows filtered, but without pagination $countFiltered = $query->count(); // Execute pagination $query->skip($start)->take($length); // Check if an order by was submitted if ($order) { $query->orderBy($order['name'], $order['dir']); } return ['rows' => $query->get(), 'count_filtered' => $countFiltered, 'count_total' => $countTotal]; }
/** * check if source is valid, then detect items count * * @mixed \Exception | int */ protected function getCount() { if (is_array($this->source)) { return count($this->source); } elseif (is_object($this->source)) { foreach ($this->valid_sources as $valid) { if (is_a($this->source, $valid)) { return $this->query->count(); } } } throw new \Exception(' "source" must be a table name, an eloquent model or an eloquent builder. you passed: ' . get_class($this->source)); }
/** * Возвращает коллекцию в виде пагинации * * @param int $page * @param int $limit */ public function pagination($page, $limit = 10) { /** * @var \Illuminate\Support\Collection $data */ $countTotal = $this->_query->count(); $this->_query->skip($limit * $page - $limit); $this->_query->limit($limit); $data = collect(); foreach ($this->_query->get() as $key => $instance) { $_listRow = []; foreach ($this->columns->getColumns() as $column) { $_listRow[$column->getKey()] = $column->getValues($instance); } $buttons = $this->filterAction($instance); if (count($buttons)) { $_listRow = array_merge($_listRow, [GridColumn::ACTION_NAME => implode('', $buttons)]); } $data->offsetSet($key, $_listRow); } return new \Illuminate\Pagination\LengthAwarePaginator($data, $countTotal, $limit, $page, ['path' => \Illuminate\Pagination\Paginator::resolveCurrentPath(), 'pageName' => 'page']); }
/** * Retrieve the "count" result of the query. * * @param string $columns * @return int * @static */ public static function count($columns = '*') { return \Illuminate\Database\Query\Builder::count($columns); }
public function build() { if (is_string($this->source) && strpos(" ", $this->source) === false) { //tablename $this->type = "query"; $this->query = $this->table($this->source); } elseif (is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Model")) { $this->type = "model"; $this->query = $this->source; $this->key = $this->source->getKeyName(); } elseif (is_a($this->source, "\\Illuminate\\Database\\Eloquent\\Builder")) { $this->type = "model"; $this->query = $this->source; $this->key = $this->source->getModel()->getKeyName(); } elseif (is_a($this->source, "\\Illuminate\\Database\\Query\\Builder")) { $this->type = "model"; $this->query = $this->source; } elseif (is_a($this->source, "\\Zofe\\Rapyd\\DataFilter\\DataFilter")) { $this->type = "model"; $this->query = $this->source->query; if (is_a($this->query, "\\Illuminate\\Database\\Eloquent\\Model")) { $this->key = $this->query->getKeyName(); } elseif (is_a($this->query, "\\Illuminate\\Database\\Eloquent\\Builder")) { $this->key = $this->query->getModel()->getKeyName(); } } elseif (is_array($this->source)) { $this->type = "array"; } else { throw new DataSetException(' "source" must be a table name, an eloquent model or an eloquent builder. you passed: ' . get_class($this->source)); } //build orderby urls $this->orderby_uri_asc = $this->url->remove('page' . $this->cid)->remove('reset' . $this->cid)->append('ord' . $this->cid, "-field-")->get() . $this->hash; $this->orderby_uri_desc = $this->url->remove('page' . $this->cid)->remove('reset' . $this->cid)->append('ord' . $this->cid, "--field-")->get() . $this->hash; //detect orderby $orderby = $this->url->value("ord" . $this->cid); if ($orderby) { $this->orderby_field = ltrim($orderby, "-"); $this->orderby_direction = $orderby[0] === "-" ? "desc" : "asc"; if ($this->canOrderby($this->orderby_field)) { $this->orderBy($this->orderby_field, $this->orderby_direction); } } //build subset of data switch ($this->type) { case "array": //orderby if (isset($this->orderby)) { list($field, $direction) = $this->orderby; $column = array(); foreach ($this->source as $key => $row) { $column[$key] = is_object($row) ? $row->{$field} : $row[$field]; } if ($direction == "asc") { array_multisort($column, SORT_ASC, $this->source); } else { array_multisort($column, SORT_DESC, $this->source); } } $limit = $this->limit ? $this->limit : 100000; $current_page = $this->url->value('page' . $this->cid, 0); $offset = max($current_page - 1, 0) * $limit; $this->data = array_slice($this->source, $offset, $limit); $this->total_rows = count($this->source); $this->paginator = new LengthAwarePaginator($this->data, $this->total_rows, $limit, $current_page, ['path' => Paginator::resolveCurrentPath(), 'pageName' => "page" . $this->cid]); break; case "query": case "model": $this->total_rows = $this->query->count(); //orderby if (isset($this->orderby)) { $this->query = $this->query->orderBy($this->orderby[0], $this->orderby[1]); } //limit-offset if (isset($this->limit)) { $this->paginator = $this->query->paginate($this->limit, ['*'], 'page' . $this->cid); $this->data = $this->paginator; } else { $this->data = $this->query->get(); } break; } return $this; }
/** * Get the entry count. * * @param array $columns * @return int */ public function count(array $columns = ['*']) { return (new Decorator())->decorate($this->query->count($columns)); }
public function it_counts_username_occurences(Model $model, Builder $builder) { $builder->where('username', 'johndoe')->shouldBeCalled(); $builder->count()->shouldBeCalled(); $this->countOccurances('johndoe'); }
/** * Retrieve the "count" result of the query, * also strips off any orderBy clause. * * @param string $columns * @return int */ public function count($columns = '*') { $previousOrders = $this->orders; $this->orders = null; $result = parent::count($columns); $this->orders = $previousOrders; return $result; }
/** * get list * * @param string $columns get columns list * @return array|static[] */ public function count($columns = '*') { if ($this->dynamic === false) { return parent::count($columns); } if ($this->proxy === true) { $this->getProxyManager()->get($this); } return parent::count($columns); }