Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dhanangpratama/c3189fe5c453c79d3d0e2e871d93bc31 to your computer and use it in GitHub Desktop.
Save dhanangpratama/c3189fe5c453c79d3d0e2e871d93bc31 to your computer and use it in GitHub Desktop.
Create pagination from array data in laravel
<?php
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
use Illuminate\Http\Request;
class PaginationSample extends Controller
{
public function index(Request $request)
{
$data = ['Dhanang', 'Andi', 'Rudi', 'Anton'];
$currentPage = Paginator::resolveCurrentPage();
$col = collect($data);
$perPage = 10;
$currentPageItems = $col->slice(($currentPage - 1) * $perPage, $perPage)->all();
$items = new Paginator($currentPageItems, count($col), $perPage);
$items->setPath($request->url());
$items->appends($request->all());
// add in view
// {!! $items->links() !!}
return view('index', compact('items'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment