Filters eloquent results

@return \Illuminate\Pagination\LengthAwarePaginator
965168
protected static function filter($priority = null, $search = null)
    {
        $key    = uri() . '.' . ActivityTypeWidgetFilter::class;
        $values = app('request')->session()->get($key);
        $userId = from_route('user');

        $ignore = config('logger.ignore_logs_in_widget');
        $query  = \Antares\Logger\Model\Logs::withoutGlobalScopes()
                ->select(['tbl_logs.*'])
                ->leftJoin('tbl_log_types', 'tbl_logs.type_id', 'tbl_log_types.id')
                ->leftJoin('tbl_logs_translations', 'tbl_logs.id', 'tbl_logs_translations.log_id')
                ->where('tbl_logs_translations.lang_id', lang_id())
                ->where('tbl_logs.brand_id', brand_id())
                ->where('tbl_log_types.active', 1);
        if (!empty($ignore)) {
            //$query->whereNotIn('tbl_log_types.name', $ignore);
        }


        $admin = user()->hasRoles('super-administrator') or user()->hasRoles('administrator');
        if (!$admin) {
            $uid       = auth()->user()->id;
            $query->whereRaw('(tbl_logs.user_id=' . $uid . ' or tbl_logs.author_id=' . $uid . ')');
            $priorites = LogPriorities::where('num', '<=', '3')->get()->pluck('id')->toArray();
            $query->where('tbl_logs.created_at', '>=', date('Y-m-d H:i:s', time() - 172800))->whereIn('priority_id', $priorites);
        } elseif (is_null($userId) && $admin) {
            $roles  = auth()->user()->roles;
            $childs = [];
            foreach ($roles as $role) {
                $childs = array_merge($childs, $role->getChilds(), [$role->id]);
            }
            $query->leftJoin('tbl_user_role', 'tbl_logs.user_id', 'tbl_user_role.user_id');
            $query->where(function($query) use($childs) {
                $query->whereIn('tbl_user_role.role_id', array_values($childs))->orWhere('tbl_logs.user_id', null);
            });
            $priorites = LogPriorities::where('num', '<=', '3')->get()->pluck('id')->toArray();
            $query->where('tbl_logs.created_at', '>=', date('Y-m-d H:i:s', time() - 172800))
                    ->whereIn('priority_id', $priorites);
        } else {
            $query->where('tbl_logs.user_id', $userId);
        }

        if (!empty($values)) {
            $query->whereIn('tbl_logs.type_id', array_values($values['value']));
        }

        if (!is_null($search) && strlen($search) > 0) {
            $searchUp   = mb_strtoupper($search);
            $searchDown = mb_strtolower($search);
            $query->whereRaw("(tbl_logs_translations.raw like  \"%{$searchUp}%\" OR tbl_logs_translations.raw like  \"%{$searchDown}%\")");
        }
        $logs = $query->orderBy('tbl_logs.created_at', 'desc')->paginate(Request::get('per_page', self::perPage));

        if (!is_null($userId)) {
            $logs->setPath(self::$baseUrl . '/' . $userId . '/');
        } else {
            $logs->setPath(self::$baseUrl);
        }

        return $logs;
    }
1156649
public function paginate($num = 15)
	{
		$page = (int) Input::get('page', 1);
		
		$this->limit($num, ($page - 1) * $num);
		
		return new LengthAwarePaginator($this->get(), $this->count(), $num, $page);
	}
1026148
private function getPagination()
    {
        # pagination
        $this->pagination['page'] = 1;
        $this->pagination['per_page'] = Utils::findKey($this->options, 'limit', 50);
        $this->pagination['padding'] = Utils::findKey($this->options, 'padding', 0);
        $this->pagination['offset'] = Utils::findKey($this->options, 'offset', 0);

        $this->payload['size'] = $this->pagination['per_page'];
        $this->payload['from'] = $this->pagination['offset'];
    }
380302
public function index()
    {
        $metrics = Metric::query();

        if ($sortBy = Binput::get('sort')) {
            $direction = Binput::has('order') && Binput::get('order') == 'desc';

            $metrics->sort($sortBy, $direction);
        }

        $metrics = $metrics->paginate(Binput::get('per_page', 20));

        return $this->paginator($metrics, Request::instance());
    }
172377
public function paginatorInfoResolver(LengthAwarePaginator $root): array
    {
        return [
            'count' => $root->count(),
            'currentPage' => $root->currentPage(),
            'firstItem' => $root->firstItem(),
            'hasMorePages' => $root->hasMorePages(),
            'lastItem' => $root->lastItem(),
            'lastPage' => $root->lastPage(),
            'perPage' => $root->perPage(),
            'total' => $root->total(),
        ];
    }
1103816
public function paginate($perPage = 25, $currentPage = null, array $options = [])
    {
        $currentPage = $this->getPageFromInput($currentPage, $options);
        $offset      = ($currentPage - 1) * $perPage;
        $total       = $this->count();
        $entries     = $this->get()->slice($offset, $perPage)->all();

        return new LengthAwarePaginator($entries, $total, $perPage, $currentPage, $options);
    }
912573
public function selectAllPaginate(
        $first = 0,
        $max = 20,
        array $filters = [],
        array $orderBy = [],
        array $groupBy = [],
        $fetchJoinCollection = true
    ) {
        $selection = $this->getEntityManager()->createQueryBuilder();

        $selection->select([$this->entityAlias])
            ->from($this->entityClass, $this->entityAlias);

        $selection = $this->filterFurther($filters, $orderBy, $groupBy, $selection, $first, $max);

        return new Paginator($selection, $fetchJoinCollection);
    }
895524
public function LengthAwarePaginatorInit($obj)
  {
    $this->pageArray['now'] = intval($obj->currentPage());
    $this->pageArray['count'] = intval($obj->total());
    $this->pageArray['size'] = intval($obj->perPage());
    $this->pageArray['end'] = intval($obj->lastPage());
    $this->setup();
    $this->lists = $obj->getCollection();
    return $this;
  }
1322141
public function filter()
    {
        return (object) [
            'type' => $this->_indexType,
            'filters' => $this->_comparisons,
            'order' => $this->_orderBy,
            'reverse' => $this->_reverse,
            'offset' => $this->_offset,
            'length' => $this->_length
        ];
    }
130974
public function getPagination()
    {
        $limit = $this->userManager->getUsersLimit();
        $count = $this->countVisibleUsers();
        $pages = $count / $limit;
        $remainder = $count % $limit;
        if ($remainder === 0) {
            return $pages;
        } else {
            return floor($pages) + 1;
        }
    }
1119874
private function generatedPresent(LengthAwarePaginator $paginator): Collection
    {
        return collect($paginator->items())
            ->map(function ($item) {
                return (object) [
                    'title'    => $item->searchTitle(),
                    'subTitle' => $item->searchSubTitle(),
                    'url'      => $item->searchUrl(),
                    'avatar'   => $item->searchAvatar(),
                ];
            });
    }
756247
public function pagination()
    {
        if ($this->pagination === null) {
            $this->pagination = $this->createPagination();
        }

        return $this->pagination;
    }
104561
private function query($count)
    {
        $this->applyQueryFilters();

        if ($this->hasTranslating()) {
            $this->applyTranslationFilter();
        }

        if ($this->orderBy()) {
            $this->query = $this->query->orderBy(
                                $this->orderBy(),
                                $this->sortBy()
                            );
        }

        if ($count) {
            return $this->query->count();
        }

        if ($this->perPage > 0) {
            return $this->query->paginate($this->perPage);
        }

        return $this->query->get();
    }
213603
public function getManyPublishedFrom(Builder $query, QueryBuilder $builder): LengthAwarePaginator
    {
        $query = $query->where('published_at', '<=', Carbon::now())
            ->join('routes as r', function ($join) {
                $join->on('contents.id', '=', 'r.routable_id')
                    ->where('r.routable_type', '=', Content::class)
                    ->where('r.is_active', true);
            })
            ->distinct();

        if ($builder->hasFilter('type') || $builder->hasSort('type')) {
            $query->join('content_types as ct', 'contents.type_id', '=', 'ct.id');
            optional($builder->getFilter('type'))->apply($query, 'ct', 'name');
            optional($builder->getSort('type'))->apply($query, 'ct', 'name');
        }

        $count = clone $query->getQuery();

        $results = $query->limit($builder->getPageSize())
            ->offset($builder->getPageSize() * ($builder->getPage() - 1))
            ->get(['contents.*']);

        $results->load(
            array_merge(
                self::$loadRelations,
                [
                    'routes' => function ($query) {
                        $query->where('is_active', true);
                    }
                ]
            )
        );

        $results->transform(function ($content) {
            $languages            = $content->routes->pluck('language_code');
            $filteredTranslations = $content->translations->filter(function ($translation) use ($languages) {
                return $languages->contains($translation->language_code);
            });
            $content->setRelation('translations', $filteredTranslations);
            return $content;
        });

        return new LengthAwarePaginator(
            $results,
            $count->select('contents.id')->get()->count(),
            $builder->getPageSize(),
            $builder->getPage()
        );
    }
1001758
public function paginate($perPage = null, $columns = ['*'])
    {
        $total = $this->query->getCountForPagination();

        $this->query->forPage(
            $page = Paginator::resolveCurrentPage(),
            $perPage = $perPage ?: $this->entityMap->getPerPage()
        );

        return new LengthAwareEntityPaginator($this->get($columns)->all(), $total, $perPage, $page, [
            'path' => Paginator::resolveCurrentPath(),
        ]);
    }
342642
public function results()
    {

        if ($this->classname) {
            $orderAdapter = app(\Antares\Datatables\Adapter\OrderAdapter::class);
            $orderAdapter->setClassname($this->classname);
            $orderAdapter->setEngineInstance($this);

            $groupsFilterAdapter = app(\Antares\Datatables\Adapter\GroupsFilterAdapter::class);
            $groupsFilterAdapter->setClassname($this->classname);
            $groupsFilterAdapter->setEngineInstance($this);
        }
        return $this->query->get();
    }
891596
public function getPaginators()
    {
        if (!isset($this->paginators)) {
            $res = call_user_func(
                $this->apiProvider,
                'paginator',
                $this->serviceName,
                $this->apiVersion
            );
            $this->paginators = isset($res['pagination'])
                ? $res['pagination']
                : [];
        }

        return $this->paginators;
    }