If you want to add next previous button on pagination in Laravel 6?, then read this article, you learn here how to create pagination in Laravel with next and previous button. We can customize the pagination link with the next and previous buttons in Laravel 6.
Laravel provide prebuilt function for pagination simplePaginate(), paginate(). In this example, the only argument passed to the paginate method is the number of items you would like displayed “per page”.
Laravel provides new eloquent method simplePaginate() for adding simple pagination with only next previous button link. for that follow bellow step and make.
Step 1:- In the first step, we need to create routes for getting data and view it.
1 2 3 | Route::get('posts', 'PostController@index'); |
Step 2:- Now, we need to create new PostController with index() method. in an index() we will write pagination code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Post; class PostController extends Controller { /** * The attributes that are mass assignable. * * @var array */ public function index() { $posts = Post::simplePaginate(5); return view('posts', compact('posts')); } } |
Step 3:- In the last step, we need to create a blade/View file and display posts with pagination.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <!DOCTYPE html> <html> <head> <title>Laravel Next Previous Link Button Pagination - Webprepration.com/</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> </head> <body> <div class="container"> <h1>Laravel Next Previous Link Button Pagination - Webprepration.com/</h1> <table class="table table-bordered"> <tr> <th>ID</th> <th>Title</th> <th>Body</th> </tr> @foreach($posts as $post) <tr> <td>{{ $post->id }}</td> <td>{{ $post->title }}</td> <td>{{ $post->body }}</td> </tr> @endforeach </table> {{ $posts->links() }} </div> </body> </html> |
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.