Skip to content

Instantly share code, notes, and snippets.

@fearrr
Created June 29, 2017 21:17
Show Gist options
  • Save fearrr/b7a9c369d93c2e7040487b962a866a52 to your computer and use it in GitHub Desktop.
Save fearrr/b7a9c369d93c2e7040487b962a866a52 to your computer and use it in GitHub Desktop.
<?php
/**
* @param $items //Принимает коллекцию или массив
* @param $page //Принимает $request->page
* @param $perPage //Принимает кол-во элементов на странице
* @param $url //Принимает $request->url()
* @param $query //Принимает $request->query()
* @return LengthAwarePaginator //Возвращает объект Paginator
*/
public function paginate($items, $page = null, $perPage = 15, $url, $query){
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator(
$items->forPage($page, $perPage),
$items->count(), // Элементов всего
$perPage, // Элементов на страницу
$page, // Текущая страница
[
'path' => $url,
'query' => $query
] // Параметры текущего запроса
);
}
//Пример вызова
$items_paginate = $this->paginate($items, $request->page, 30, $request->url(), $request->query());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment