Laravel 8 Tailwind Pagination Example Tutorial

10-Apr-2023

.

Admin

Laravel 8 Tailwind Pagination Example Tutorial

Hi Dev,

In this example I will explain to you how to use tailwind pagination with laravel 8. this example will help you laravel create a tailwind pagination example.

I will explain how to create tailwind CSS laravel 8 pagination. we will create simple and attractive pagination using Tailwind CSS so follow my steps.

So, you can use this tutorial code and you can implement your laravel project.

Here, I will show you a full example of creating tailwind pagination in laravel so follow mine below steps.

Step: 1 Add Route


First of all, we put one route in one for list users with pagination. So simply add both routes to your route file.

/routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\UserController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

// UserController

Route::get('users', [UserController::class, 'index'])->name('users');

Step: 2 Create Controller

In this second step, we will create new Controller file to handle request of created new route. In this Controller we define two method, index() for listing for users. this method will handle route request. So let's create new controller and put code.

/app/Http/Controllers/UserController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class UserController extends Controller

{

/**

* Write Your Code..

*

* @return string

*/

public function index(Request $request)

{

$users = User::latest()->paginate(10);

return view('user',compact('users'));

}

}

Step 3: Create Blade File

In this step we will create user.blade.php with tailwind following folder.

resources/views/user.blade.php

@extends('layouts.app')

@section('style')

<style type="text/css">

.duration-150{

padding: 8px 15px !important;

}

</style>

@endsection

@section('content')

<div class="container">

<div class="row">

<div class="col-md-12 my-2">

<p class="font-weight-bold" style="font-size: 30px;">Laravel 8 Tailwind Pagination Example Tutorial- <span class="text-primary">Nicesnippets.com</span></p>

</div>

</div>

<table class="table table-bordered table-sm">

<thead class="bg-dark text-white">

<tr>

<th>No</th>

<th>Name</th>

<th>Email</th>

</tr>

</thead>

@foreach ($users as $key => $value)

<tbody>

<tr>

<td>{{ ++$key }}</td>

<td>{{ $value->name }}</td>

<td>{{ $value->email }}</td>

</tr>

</tbody>

@endforeach

</table>

<div class="row">

<div class="col-md-12">

{{ $users->links('pagination::tailwind') }}

</div>

</div>

</div>

@endsection

Step 4: Create Pagination Template

this step we are going to use laravel custom pagination template, that's why run below command to have it.

php artisan vendor:publish --tag=laravel-pagination

Include Tailwind CSS Style using CDN inside the head tag.

<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">

This command will place the views in your application's resources/views/vendor/pagination directory. The tailwind.blade.php file within this directory corresponds to the default pagination view. You may edit this file to modify the pagination HTML.

resources/views/vendor/pagination/tailwind.blade.php

@if ($paginator->hasPages())

<nav role="Page navigation" aria-label="{{ __('Pagination Navigation') }}" class="flex items-center justify-between">

<div class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between">

<div>

<p class="text-sm text-gray-700 leading-5">

{!! __('Showing') !!}

<span class="font-medium">{{ $paginator->firstItem() }}</span>

{!! __('to') !!}

<span class="font-medium">{{ $paginator->lastItem() }}</span>

{!! __('of') !!}

<span class="font-medium">{{ $paginator->total() }}</span>

{!! __('results') !!}

</p>

</div>

<div>

<span class="relative z-0 inline-flex shadow-sm rounded-md">

{{-- Previous Page Link --}}

@if ($paginator->onFirstPage())

<button class="flex items-center justify-center w-10 h-10 text-green-600 transition-colors duration-150 rounded-full focus:shadow-outline hover:bg-green-100">

<svg class="w-4 h-4 fill-current" viewBox="0 0 20 20"><path d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" fill-rule="evenodd"></path></svg>

</button>

@else

<a href="{{ $paginator->previousPageUrl() }}" rel="prev" class="flex items-center justify-center w-10 h-10 text-green-600 transition-colors duration-150 rounded-full focus:shadow-outline hover:bg-green-100" aria-label="{{ __('pagination.previous') }}">

<svg class="w-4 h-4 fill-current" viewBox="0 0 20 20"><path d="M12.707 5.293a1 1 0 010 1.414L9.414 10l3.293 3.293a1 1 0 01-1.414 1.414l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 0z" clip-rule="evenodd" fill-rule="evenodd"></path></svg>

</a>

@endif

{{-- Pagination Elements --}}

@foreach ($elements as $element)

{{-- "Three Dots" Separator --}}

@if (is_string($element))

<span aria-disabled="true">

<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-700 bg-white border border-gray-300 cursor-default leading-5">{{ $element }}</span>

</span>

@endif

{{-- Array Of Links --}}

@if (is_array($element))

@foreach ($element as $page => $url)

@if ($page == $paginator->currentPage())

<span aria-current="page">

<button class="w-10 h-10 text-white transition-colors duration-150 bg-green-600 border border-r-0 border-green-600 rounded-full focus:shadow-outline">{{ $page }}</button>

</span>

@else

<a href="{{ $url }}" class="w-10 h-10 text-green-600 transition-colors duration-150 rounded-full focus:shadow-outline hover:bg-green-100" aria-label="{{ __('Go to page :page', ['page' => $page]) }}">

{{ $page }}

</a>

@endif

@endforeach

@endif

@endforeach

{{-- Next Page Link --}}

@if ($paginator->hasMorePages())

<a href="{{ $paginator->nextPageUrl() }}" rel="next" class="flex items-center justify-center w-10 h-10 text-green-600 transition-colors duration-150 rounded-full focus:shadow-outline hover:bg-green-100" aria-label="{{ __('pagination.previous') }}">

<svg class="w-4 h-4 fill-current" viewBox="0 0 20 20"><path d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd" fill-rule="evenodd"></path></svg>

</a>

@else

<button class="flex items-center justify-center w-10 h-10 text-green-600 transition-colors duration-150 bg-white rounded-full focus:shadow-outline hover:bg-green-100">

<svg class="w-4 h-4 fill-current" viewBox="0 0 20 20"><path d="M7.293 14.707a1 1 0 010-1.414L10.586 10 7.293 6.707a1 1 0 011.414-1.414l4 4a1 1 0 010 1.414l-4 4a1 1 0 01-1.414 0z" clip-rule="evenodd" fill-rule="evenodd"></path></svg>

</button>

@endif

</span>

</div>

</div>

</nav>

@endif

Preview

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/users

It will help you...

#Laravel 8

#Laravel 7

#Laravel

#Laravel 6