Pagination Example In Laravel

Websolutionstuff | Sep-24-2020 | Categories : Laravel

In this article, we will see a pagination example in laravel, as we all know pagination is a very common feature in all websites, if we want to display a specific number of details or images then we can use pagination. Laravel provides paginate method and it will automatically take care of setting the proper limit and offset based on the current page being viewed by the user. Here we will show you how to use pagination in laravel 6 and laravel 7. Also, we have to try to paginate method in laravel 6 and laravel 7.

So, let's see laravel custom pagination, laravel pagination links, and pagination in laravel 6/7/8.

First of all, we will create a controller and add the below code in the index() function, and we will use paginate() function for pagination.

app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

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

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

 

 

Now, we need to create a blade file for viewing. here we will add the links() function. So, it will generate pagination automatically.

resources/views/index.blade.php

@extends('users.layout')
@section('content')
    <div class="row">
        <div class="col-lg-12" style="margin-top: 15px ">
            <div class="text-center">
                <h2>Pagination Example in Laravel - websolutionstuff.com</h2>
            </div>
        </div>
    </div><br>
   
    <table class="table table-bordered">
        <tr>
            <th>Name</th>
            <th>Email</th>
        </tr>
        @if(!empty($users))
        @foreach ($users as $user)
        <tr>
            <td>{{ $user->name }}</td>
            <td>{{ $user->email }}</td>
        </tr>
        @endforeach
        @else
            <tr>
              <td colspan="4">No data found</td>  
            </tr>
        @endif
    </table>
  
    <div class="text-center">
        {!! $users->links() !!}
    </div>
@endsection

 

 

pagination-example-in-laravel

 


You might also like:

Recommended Post
Featured Post
jQuery Image Magnifier on Mouse Hover
jQuery Image Magnifier on Mous...

In this article, we will see a jquery image magnifier on mouse hover. Using an image magnifier you can enlarge...

Read More

Jan-04-2021

How To Integrate Paypal Payment Gateway In Laravel
How To Integrate Paypal Paymen...

In this tutorial I will teach you the most important topic of how to integrate the PayPal payment gateway in larave...

Read More

Jul-22-2020

How To Add Action Button In Angular 15 Material Datepicker
How To Add Action Button In An...

In this tutorial, I will guide you through the process of adding an action button to the Angular 15 Material datepicker...

Read More

Jul-07-2023

What Is New In Laravel 9 - Features Of Laravel 9
What Is New In Laravel 9 - Fea...

In this article, we will see what is new in laravel 9 and the features of laravel 9. Laravel 9 is recently released...

Read More

Feb-13-2022