Laravel 8 Pagination Example

Laravel 8 Pagination Example

Hello friends, Today we will show Laravel 8 Pagination Example Tutorial. In this post we will explain you from scratch how to working with laravel 8 pagination. Also we will explain how to use pagination in laravel 8 application.

Here in this example we will show laravel 8 pagination with user table. We will share with you example of laravel 8 pagination blade example. In this post we will discus on laravel 8 pagination tutorial.

You always need laravel 8 pagination for handling a large number of data in your application. We will step by step guide to laravel 8 pagination tutorial. so let’s following to create the pagination in Laravel 8.

Read Also :  Laravel Eloquent firstOr() Example

Laravel 8 Pagination Example Tutorial

Step 1: Add Route

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\UserController;
  
Route::get('users', [UserController::class, 'index']);

Step 2: Create Controller for Laravel 8 Pagination

app/Http/Controllers/UserController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\User;
  
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = User::paginate(5);
        return view('users',compact('data'));
    }
}

Step 3: Create Blade File

In this step, you required to create users blade file and put following code with links() so it will generate pagination automatically. so let’s put it.

resources/views/users.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 8 CRUD Application - web-tuts.com</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
    
<div class="container">
    <h1>Laravel 8 Pagination Example - web-tuts.com</h1>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>Name</th>
                <th width="300px;">Action</th>
            </tr>
        </thead>
        <tbody>
            @if(!empty($data) && $data->count())
                @foreach($data as $key => $value)
                    <tr>
                        <td>{{ $value->name }}</td>
                        <td>
                            <button class="btn btn-danger">Delete</button>
                        </td>
                    </tr>
                @endforeach
            @else
                <tr>
                    <td colspan="10">There are no data.</td>
                </tr>
            @endif
        </tbody>
    </table>
         
    {!! $data->links() !!}
</div>
     
</body>
</html>

If you are using bootstrap then you have to add useBootstrap() on service provider as like bellow:

Read Also :  Laravel Left Join Query Example

app\Providers\AppServiceProvider.php

<?php
  
namespace App\Providers;
  
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;
  
class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
    
    }
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Paginator::useBootstrap();
    }
}

Laravel 8 Pagination with appends parameter

{!! $data->appends(['sort' => 'votes'])->links() !!}

Laravel 8 Pagination with appends request all parameters

{!! $data->appends(Request::all())->links() !!}

I hope you understand or Laravel 8 Pagination and it can help you…

Read Also :  Laravel Arr sort() function Example