Simple Pagination Example in Laravel 7

In this tutorial, I will teach you how to create a simple pagination in Laravel 7

Generally, We know pagination is a basic requirement of every project. Laravel 7 Provides direct function of pagination which are we use in our tutorial.

So let’s start our tutorial


Step 1: Create a dummy record.

We have to create a dummy record to use in pagination.

php artisan tinker
factory(App\User::class, 100)->create();

Step 2: Create a Route

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

Step 3: Create Controller

Create a Controller in app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

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

class UserController extends Controller{
    public function index(){
        $users = User::paginate(10);
	return view('users',compact('users'));
    }    
}

Step 4: Create Blade File

Create a blade file in resources/views/users.blade.php

@extends('layouts.app')
@section('content')
	<table class="table table-bordered">
	    <thead>
	        <tr>
	            <th>Name</th>
	            <th>Action</th>
	        </tr>
	    </thead>
	    <tbody>
	        @if(!empty($users))
	            @foreach($users as $user)
	                <tr>
	                    <td>{{ $user->name }}</td>
	                    <td>
	                        <button class="btn">Delete</button>
	                    </td>
	                </tr>
	            @endforeach
	        @else
	            <tr>
	                <td>There are no data.</td>
	            </tr>
	        @endif
	    </tbody>
	</table>
	{!! $users->links() !!}	
@endsection

Now open your browser and put the URL.

http://127.0.0.1:8000/users

Stay Safe! Stay Healthy!

About Dhaval Shah

My name is Dhaval Shah. I'm a Laravel developer. I live in India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Java-script, JQuery, Laravel, WordPress, and Bootstrap.
Follow me on Github / Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!