Laravel Pagination Next Previous Button Example Tutorial


Share:
Published By: Admin, Published On: Jun 26, 2021, Category: Laravel 8 Laravel

Hi Dev,

Today, In this article i would like to share you how to create next previous button in laravel you can easily create next previous button in laravel 8 application.

So, we can add next and previous link on pagination utilizing simplePaginate() in laravel 8 application. laravel provide new eloquent method simplePaginate() for integrate simple pagination with only next previous button link.

Here, I will give you simple example of create next previous button in laravel so follow my steps.

Create Route Path : routes/web.php
<?php
use App\Http\Controllers\HomeController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('previous-next', [HomeController::class, 'index']);
Create Controller:

Next, In this step we will create a simple home controller just following command through.

Path : app/Http/Controllers/HomeController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class HomeController extends Controller
{
    /**
     * Write Your Code..
     *
     * @return string
    */
    public function index()
    {
        $users = User::simplePaginate(5);
        return view('users', compact('users'));
    }
}

Create Blade File

After Successfully create controller we can create a simpele blade file users.blade.php

Path : resources/views/users.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Laravel Next Previous Link Button Pagination - MyWebTuts.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body class="bg-dark"> 
   
<div class="container mt-5">
    <div class="row">
        <div class="col-md-8 offset-2">
            <div class="card">
                <div class="card-header">
                   <h5>Laravel Next Previous Link Button Pagination - MyWebTuts.com</h5>
                </div>
                <div class="card-body">
                    <table class="table table-bordered table-hover">
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                        </tr>
                        @foreach($users as $user)
                        <tr>
                            <td>{{ $user->id }}</td>
                            <td>{{ $user->name }}</td>
                        </tr>
                        @endforeach
                    </table>
                    {{ $users->links() }}
                </div>
            </div>
        </div>
    </div>
</div>

</body>
</html>

It will help you...

Share: