/** * 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>'; }
/** * Get the number per page. * * @return int */ public function getPerPage() { return $this->paginator->perPage(); }
/** * 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; }
/** * Is this current page in range? * * @param \Illuminate\Pagination\LengthAwarePaginator $paginator * * @return bool */ protected function isPageInRange(LengthAwarePaginator $paginators) { return $paginators->currentPage() <= ceil($paginators->lastItem() / $paginators->perPage()); }
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); }