Site Admin
Site Admin Founder of MeaningArticles
1045 Views

Laravel 8 simple pagination example

Hello Dev.

Nowadays, we can explain to you how to create pagination in laravel 8. so that you can see our laravel 8 pagination instance.

Pagination means document content material dividing into multiple pages. every time we've one lengthy page at that point we dividing into multiple pages. however, laravel affords the in-build functionality for pagination like as paginate(), count() and link(). we simply use of this functionality.

So, you could see below the example to create simple pagination in laravel 8(laravel pagination).

let’s start with below step to create simple pegination in laravel 8.

So let's start the lesson...

 

Step 1: Install Laravel 8

First of all we want to get clean and new Laravel application the use of bellow command, now open your terminal OR command prompt and then fire bellow command and run it:

composer create-project --prefer-dist laravel/laravel laravel_pagination_post


Step 2: Create Routes

Add the subsequent route code inside the “routes/web.php” file.

web.php

<?php
use App\Http\Controllers\UsersController;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| here is wherein you may register web routes for your application. these
| routes are loaded with the aid of the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something tremendous!
|
*/ 
Route::get('user', [UsersController::class, 'index']);
?>

 

Step 3: Create UsersController

Right here in this step, we will create the UsersController and index method. after then we can return the limit data for the view file.
you can use the below command use to creating a controller.

php artisan make:controller UsersController

UsersController.php

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


Step 4: Create Blade File

Finally, we are able to create a pagination.blade.php file in the “resources/views/” folder directory and paste the below code. right here, in this file we use the {!! $data->links() !!} for pagination.
pagination.blade.php

<!DOCTYPE html>
<html lang="en">

<head>
  <title>Laravel 8 Pagination Example - meaningarticles.com</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <table class="table table-bordered" id="laravel">
                   <thead>
                      <tr>
                         <th>Id</th>
                         <th>Name</th>
                         <th>Email</th>
                         <th>Created at</th>
                      </tr>
                   </thead>
                   <tbody>
                        @if(!empty($data) && $data->count())
                          @foreach($data as $user)
                          <tr>
                             <td>{{ $user->id }}</td>
                             <td>{{ $user->name }}</td>
                             <td>{{ $user->email }}</td>
                             <td>{{ date('d m Y', strtotime($user->created_at)) }}</td>
                          </tr>
                          @endforeach
                        @else
                        <tr>
                            <td colspan="4">No data found.</td>
                        </tr>
                        @endif
                   </tbody>
                </table>
            </div>
        </div>    
        <div class="row">  
            <div class="col-lg-12">
                {!! $data->links() !!}
            </div>
        </div>
    </div>
</body>  
</html>

 

Step 5: Run Laravel Application

Execute the following command to begin the Laravel pagination demo project.

php artisan serve

You check the app on http://127.0.0.1:8000/user

I hope it assists you, thanks for visiting my article if you like my article then share it with your friends on the social media platform.

Happy Coding.....