Pagination Example In Laravel 7/8

In this article I will give you pagination example in laravel 7/8, Pagination is very common feature in all websites, if we want to display specific number of details or images then we can use pagination. Laravel provide paginate method and it will automatically takes care of setting the proper limit and offset based on the current page being viewed by the user.

There are several ways to paginate items. The simplest is by using the paginate method in laravel 6/7/8. The paginate method counts the total number of records matched by the query before retrieving the records from the database. Here I will show you how to use pagination in laravel 6/7/8.

Let's see pagination example in laravel 6/7/8.

So, first of all we will create controller and add below code in index() function, and I have used paginate() function for pagination. In this example, the only argument passed to the paginate method is the number of items you would like displayed "per page". In this case, let's specify that we would like to display 15 items per page.

 

 

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 blade file for view or output, here I have added 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 7/8 - Techsolutionstuff</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

 

 

Output :

pagination_example_in_laravel_7_8_output

So, this is very simple and basic level of laravel 7/8 pagination example, if you want to advance level pagination then you can find on laravel official website Pagination.

 


You might also like :

RECOMMENDED POSTS

FEATURE POSTS