Laravel Next Previous Link Button Pagination (ok)

https://www.itsolutionstuff.com/post/laravel-next-previous-link-button-paginationexample.html

Vi du da hoan thanh

C:\xampp\htdocs\reset\routes\web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
    return view('welcome');
});
Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('/users', [UserController::class, 'index']);

C:\xampp\htdocs\reset\app\Http\Controllers\UserController.php

<?php
namespace App\Http\Controllers;
use App\Models\User;
class UserController extends Controller {
  /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
  public function index() {
    $users = User::simplePaginate(5);
    return view('users', compact('users'));
  }
}

C:\xampp\htdocs\reset\resources\views\users.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Next Previous Link Button Pagination - ItSolutionStuff.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 - ItSolutionStuff.com</h1>
    <table class="table table-bordered">
        <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>
</body>
</html>

we can add next and previous link on pagination using simplePaginate() in laravel 6, laravel 7, laravel 8 and laravel 9 application. laravel provide new eloquent method simplePaginate() for adding simple pagination with only next previous button link.

If you also want to add pagination with next and previous link then follow bellow some step and make it done as like bellow screen shot:

Create Route:

In first step, we will create simple routes for getting users and view it, so let's add new route in web.php file:

routes/web.php

Route::get('users', 'UserController@index');

Create Controller:

Now, we will create new UserController with index() method. in index() we will write simple pagination code. so let's create as like bellow:

app/Http/Controllers/UserController.php

<?php   namespace App\Http\Controllers;   use Illuminate\Http\Request;use App\User;   class UserController extends Controller{    /**     * The attributes that are mass assignable.     *     * @var array     */    public function index()    {        $users = User::simplePaginate(5);        return view('users', compact('users'));    }}

Create View File:

In this last step, we will create simple blade file and display users with pagination. so let's add bellow code:

resources/views/users.blade.php

Read Also: Laravel 7/6 Socialite Login with Google Gmail Account

<!DOCTYPE html><html><head>    <title>Laravel Next Previous Link Button Pagination - ItSolutionStuff.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 - ItSolutionStuff.com</h1>       <table class="table table-bordered">        <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>   </body></html>

Ok, now you have to create some dummy records on users table.

Then you can check it.

I hope it can help you...

Last updated