<?php $posts = Post::query()->paginate(); $posts = DB::table('posts')->paginate();
Create LengthAwarePaginator in Laravel
To create this pagination you can first import the class from the namespace below.
<?php use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator;
<?php use App\Models\Post use Illuminate\Pagination\Paginator; use Illuminate\Pagination\LengthAwarePaginator; Route::get('posts', function () { $posts = Post::get(); // get 1000 posts $perPage = 20; $currentPage = request("page") ?? 1; $pagination = new LengthAwarePaginator( $posts->slice($currentPage, $perPage), $posts->count(), $perPage, $currentPage, [ 'path' => request()->url(), 'query' => request()->query(), ] ); return view('posts.index', compact('posts')); });
$items, $total, $perPage, $currentPage = null, array $options = []
Leave a reply