Last active
December 3, 2024 07:02
-
-
Save dhanangpratama/c3189fe5c453c79d3d0e2e871d93bc31 to your computer and use it in GitHub Desktop.
Create pagination from array data in laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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