use Illuminate\Pagination\Paginator; $items = collect([...]); // array of items to paginate $perPage = 10; // number of items to display per page $page = Paginator::resolveCurrentPage(); // get the current page number from query string $paginatedItems = $items->slice(($page - 1) * $perPage, $perPage)->all(); // slice the collection to get current page's items $paginated = new LengthAwarePaginator($paginatedItems, count($items), $perPage, $page); // create pagination object return view('items.index', ['items' => $paginated]);
use Illuminate\Support\Facades\DB; $items = DB::table('items')->paginate(10); // paginate the query with 10 items per page return view('items.index', ['items' => $items]);
use Zend\Paginator\Paginator; use Zend\Paginator\Adapter\ArrayAdapter; $items = [...]; // array of items to paginate $perPage = 10; // number of items to display per page $page = $_GET['page'] ?? 1; // get the current page number from query string $paginator = new Paginator(new ArrayAdapter($items)); // create pagination object with array adapter $paginator->setCurrentPageNumber($page); // set the current page number $paginator->setItemCountPerPage($perPage); // set the number of items per page return view('items.index', ['items' => $paginator]);