Pagination Example In Laravel

WebSolutionStuff | Sep-24-2020 | Categories : Laravel

In this post i will show you Pagination Example In Laravel, as we all know 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.here i will show you how to use pagination in laravel,So i have try paginate method in laravel.

So, first of all we will create controller and add below code in index() function, and i have used 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 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 - 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

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

 

Featured Post




Follow us
facebooklogo github instagram twitter