Hello Devs,

In this tutorial, we are going to learn how to add pagination In Laravel 7.

Follow this step by step guide given below:




Step 1:


 Add Route routes/web.php

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



Step 2: 


Create Controller app/Http/Controllers/UserController.php

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



Step 3: 


Create Blade File resources/views/users.blade.php

@extends($theme)
  
@section('content')
  
<table class="table table-bordered">
    <thead>
        <tr>
            <th>Name</th>
            <th width="300px;">Action</th>
        </tr>
    </thead>
    <tbody>
        @if(!empty($data) && $data->count())
            @foreach($data as $key => $value)
                <tr>
                    <td>{{ $value->name }}</td>
                    <td>
                        <button class="btn btn-danger">Delete</button>
                    </td>
                </tr>
            @endforeach
        @else
            <tr>
                <td colspan="10">There are no data.</td>
            </tr>
        @endif
    </tbody>
</table>
   
{!! $data->links() !!}
  
@endsection


Pagination with appends parameter

{!! $data->appends(['sort' => 'votes'])->links() !!}


Pagination with appends request all parameters

{!! $data->appends(Request::all())->links() !!}