/** * @param LengthAwarePaginator $paginator * @param $bootstrapVersion * @return string */ protected static function _render(LengthAwarePaginator $paginator, $bootstrapVersion) { if ($paginator->lastPage() <= 1) { return ''; } switch ($bootstrapVersion) { case 4: $defaultPresenter = 'pagination::bootstrap-4'; break; default: $defaultPresenter = ''; } return $paginator->render($defaultPresenter); }
/** * Respond with a paginator, and a transformer. * * @param LengthAwarePaginator $paginator * @param callable|\League\Fractal\TransformerAbstract $transformer * @param string $resourceKey * @param array $meta * @return ResponseFactory */ public function withPaginator(LengthAwarePaginator $paginator, $transformer, $resourceKey = null, $meta = []) { $resource = new Collection($paginator->items(), $transformer, $resourceKey); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); foreach ($meta as $metaKey => $metaValue) { $resource->setMetaValue($metaKey, $metaValue); } $rootScope = $this->manager->createData($resource); return $this->withArray($rootScope->toArray()); }
/** * todo make a better html paginator * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $collection * @return string */ function paginate(\Illuminate\Contracts\Pagination\LengthAwarePaginator $collection) { $current = $collection->currentPage(); $per = $collection->perPage(); $count = $collection->count(); $from = $current * $per; $to = $from + $count; $html = ''; // "From {$from} to {$to}"; $previous = $current > 1 ? $collection->url("?page=" . ($current - 1)) : false; $next = $collection->hasMorePages() ? $collection->nextPageUrl() : false; if ($previous) { $html .= '<li><a href="' . $previous . '">prev</a></li>'; } if ($next) { $html .= '<li><a href="' . $next . '">next</a></li>'; } return '<ul>' . $html . '</ul>'; }
/** * Is the current page the first page? * * @param \Illuminate\Pagination\Paginator $paginator * * @return bool */ protected function isFirstPage(LengthAwarePaginator $paginator) { return $paginator->currentPage() === 1; }
/** * Get the page range for the current page window. * * @param int $onEachSide * @return array */ public function getAdjacentUrlRange($onEachSide) { return $this->paginator->getUrlRange($this->currentPage(), $this->currentPage()); }
/** * Create FractalCollection payload with pagination. * * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator * @param null $transformer * @param string|null $resourceKey * @return mixed */ public function getPagination(LengthAwarePaginator $paginator, $transformer = null, $resourceKey = null) { // Append existing query parameter to pagination link // @see http://fractal.thephpleague.com/pagination/#including-existing-query-string-values-in-pagination-links $queryParams = array_diff_key($_GET, array_flip(['page'])); foreach ($queryParams as $key => $value) { $paginator->addQuery($key, $value); } $collection = $paginator->getCollection(); $resource = new FractalCollection($collection, $this->getTransformer($transformer), $this->getResourceKey($resourceKey)); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); if ($meta = $this->getMeta()) { $resource->setMeta($meta); $this->meta = []; } if ($includes = $this->getIncludes()) { $this->fractal->parseIncludes($this->request->input('include')); $this->includes = null; } return $this->fractal->createData($resource)->toArray(); }
protected function prepareProductsForDisplay(LengthAwarePaginator $products) { return $products->map(function ($product) { return $this->prepareProductForDisplay($product); }); }
/** * {@inheritdoc} */ public function next() { return $this->posts->nextPageUrl(); }
/** * Get the last page from the paginator. * * @return int */ protected function lastPage() { return $this->paginator->lastPage(); }
protected function respondWithPagination(LengthAwarePaginator $lenhthAwarePaginator, $data) { $data = array_merge($data, ['paginator' => ['total_count' => $lenhthAwarePaginator->total(), 'total_pages' => ceil($lenhthAwarePaginator->total() / $lenhthAwarePaginator->perPage()), 'current_page' => $lenhthAwarePaginator->currentPage(), 'limit' => $lenhthAwarePaginator->perPage()]]); return $this->respond($data); }
/** * @param $data * @param LengthAwarePaginator $pagination * * @return array */ public static function withPaginationData($data, LengthAwarePaginator $pagination) { $data = ['data' => $data, 'pagination' => ['total_count' => $pagination->total(), 'total_pages' => ceil($pagination->total() / $pagination->perPage()), 'current_page' => $pagination->currentPage(), 'limit' => (int) $pagination->perPage()]]; return $data; }
/** * @param LengthAwarePaginator $articles Articles to change to an ArticleCollection * @return ArticleCollection */ private function paginate(LengthAwarePaginator $articles) { return new ArticleCollection($articles->items(), $articles->total(), $articles->perPage(), $articles->currentPage(), ['lastPage' => $articles->lastPage(), 'path' => $articles->resolveCurrentPath()]); }
/** * Will result in an array with a paginator * * @param LengthAwarePaginator $items The paginated items * @param array $data The data * @param array $headers The headers that should be send with the JSON-response * * @return \Illuminate\Http\JsonResponse The JSON-response with the paginated results */ protected function respondWithPagination(LengthAwarePaginator $items, $data, $headers = []) { $data = array_merge($data, ['pagination' => ['total_count' => $items->total(), 'total_pages' => $items->lastPage(), 'current_page' => $items->currentPage(), 'limit' => $items->perPage()]]); return $this->respond($data, $headers); }
/** * Make resource from an Eloquent paginator. * * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator * @return \League\Fractal\Resource\ResourceInterface */ protected function makeFromPaginator(Paginator $paginator) : ResourceInterface { $resource = static::makeFromCollection($paginator->getCollection()); if ($resource instanceof CollectionResource) { $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); } return $resource; }
/** * @param LengthAwarePaginator $paginator * @param $data * @return \Illuminate\Http\JsonResponse */ protected function respondWithPagination(LengthAwarePaginator $paginator, $data) { $paginatorArray = $paginator->toArray(); unset($paginatorArray['data']); $data = ['paginator' => $paginatorArray, 'data' => $data, 'status_code' => $this->getStatusCode()]; return $this->respond($data); }
/** * Get the url for the given page. * * @param int $page * * @return string */ public function getUrl($page) { return $this->paginator->url($page); }
/** * Returns the Pagination Data * * @param $paginatedItems * * @return array */ protected function getPaginationData(LengthAwarePaginator $paginatedItems) { $paginationInfo = ['items' => $paginatedItems->total(), 'pages' => ceil($paginatedItems->total() / $paginatedItems->perPage()), 'each' => $paginatedItems->perPage(), 'current' => $paginatedItems->currentPage()]; return $paginationInfo; }
/** * Get all urls in an array. * * @param \Illuminate\Contracts\Pagination\LengthAwarePaginator $paginator * @param bool $full Return the full version of the URL in for the first page * Ex. /users/page/1 instead of /users * * @return array */ public function allUrls(LengthAwarePaginator $paginator, $full = false) { if (!$paginator->hasPages()) { return []; } $urls = []; for ($page = 1; $page <= $paginator->lastPage(); ++$page) { $urls[] = $this->pageUrl($page, $full); } return $urls; }
public function transformPaginator(LengthAwarePaginator $items) { return $items->map([$this, 'transform']); }
/** * @param LengthAwarePaginator $paginator * @param \League\Fractal\TransformerAbstract|\Closure $transformer * @param string $resourceKey * @return mixed */ public function respondPaginatedCollection(LengthAwarePaginator $paginator, $transformer = null, $resourceKey = null) { $paginator->appends($this->request->query()); $resource = new Collection($paginator->items(), $this->getTransformer($transformer), $resourceKey); $resource->setPaginator(new IlluminatePaginatorAdapter($paginator)); $rootScope = $this->manager->createData($resource)->toArray(); return $this->respondWithArray($rootScope); }