Laravel has a lot of useful functions in the core, one of them is a simple pagination for your table list of entries. Let’s see how it works.
Let’s say we have a list table of authors, with this code.
/app/Http/Controllers/AuthorsController.php:
public function index() { $authors = Author::all(); return view('authors.index', compact('authors')); }
And /resources/views/authors/index.blade.php:
First name | Last name | Actions |
---|---|---|
{{ $author->first_name }} | {{ $author->last_name }} | Edit |
No entries found. |
Here’s the visual result of it all.
What if we have more entries and need to have pagination?
First, we use paginate() method in Controller instead of all():
public function index() { $authors = Author::paginate(5); return view('authors.index', compact('authors')); }
Next – to display the pagination in our list in the file resources/views/authors/index.php we just use method links() like this:
In case of 1-5 entries, it won’t display anything, but beyond that we will see something like this:
Guess what – if we click “Page 2” link or the arrow to the right, it will actually work!
As you can see, paginator will add a GET parameter to the URL ?page=2, and we don’t need to add any custom code to make it work. Isn’t that awesome?
Of course, you can perform a lot of customizations with this pagination – move the page length to config, change URL parameters and add your own ones, apply different styling than default etc. But I will leave it for you to play around, if you wish. More information about pagination in Laravel can be found in the official documentation.
Pagination in Laravel of this kind
1 2 3 4 5 6 7 8 9
How to make pagination in this form
1 2 3. . . 7 8 9
Thank you
Hi Vladimir,
You can customize pagination with some parameters or use specific functions instead of just ->links()
See here: https://laravel.com/docs/5.5/pagination#customizing-the-pagination-view
Paginate following type of html response return to Ajax in laravel
foreach ($products as $key => $product) {
$output.=”.
”.$product->id.”.
”.$product->title.”.
”.$product->description.”.
”.$product->price.”.
”;
}
return Response($output);
Please provide the meaning of below code:-
view(‘products.index’,compact(‘products’))
->with(‘i’, (request()->input(‘page’, 1) – 1) * 5);
I can’t get to following pages starting with 2, because I get compact(): Undefined variable: cv
public function filter(Request $request){
$request->input(‘job’) != ” ? $cv = DB::table(‘cvs’)->where(‘job’, ‘=’, $request->input(‘job’))->orderBy(‘created_at’, ‘desc’)->paginate($request[‘paging’]) : null;
return view(‘index’, compact(‘cv’));
}
I use get request
Wrong syntax, your “: null” isn’t assigned to $cv. It should be:
$cv = $request->input(‘job’) != ” ? DB::table(‘cvs’)->where(‘job’, ‘=’, $request->input(‘job’))->orderBy(‘created_at’, ‘desc’)->paginate($request[‘paging’]) : null;
Now i fix issues but I get division by zero when I complete filtered query with custom paging parameters
hi povilas,
how could we implement pagination in alphabetic order from a-z?
any tip? ty 🙂
What exactly do you mean? You mean every page would be a letter? It’s not a short answer, but maybe my tweet will help: https://twitter.com/DailyLaravel/status/1242058267825770497
How would you setup pagination for a read more instead of forward and back?
I personally hate “Read more” because it doesn’t say the number of records in total, but how would I setup – with Vue.js and API call.