How To Create Livewire Pagination Example Laravel 8


This post is easy to how to use laravel livewire pagination example. it's a simple example of laravel 8 livewire pagination tutorial. I’m going to show you laravel livewire tutorial. In this article, we will implement Livewire pagination not working.

In this tutorial, we will create simple pagination using laravel livewire using PHP artisan livewire. you can use laravel livewire pagination with all versions.

Livewire is a full-stack framework for Laravel framework then uses laravel livewire pagination that makes building dynamic interfaces and use simple, without leaving the comfort of Laravel. if you are using livewire with laravel then you don't worry about writing any code like jquery ajax etc, livewire will help to write very simple way jquery ajax code using PHP. without page refresh laravel validation will work, the form will submit, etc.

Here, I will give you a very simple example of creating pagination with a user's table and I will store that data in the database without a refresh page and too many lines of code in the blade file. we will use only the livewire/livewire package.

Livewire Pagination  / How To Create Livewire Pagination Example
Step 1 : Install Laravel 8

First of all, we need to get fresh Laravel 8 version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog
Step 2 : Create Dummy Records Using Tinker Factory

You need to run following command to create a dummy record in your users table. let's run both command:

php artisan tinker
User::factory()->count(100)->create()
Step 3: Install Livewire

Now in this step, we will simply install livewire to our laravel 8 application using the below command. see how to use livewire pagination:

composer require livewire/livewire
Step 4: Create Component

Now here we will create a livewire component using their command. so run the below command to create a pagination component.

php artisan make:livewire user-pagination


Now they created files on both paths:

app/Http/Livewire/UserPagination.php
resources/views/livewire/user-pagination.blade.php

Now both files we will update as below for our contact us form.

app/Http/Livewire/UserPagination.php

<?php
  
namespace App\Http\Livewire;
  
use Livewire\Component;
use Livewire\WithPagination;
use App\Models\User;
  
class UserPagination extends Component
{
    use WithPagination;
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function render()
    {
        return view('livewire.user-pagination', [
            'users' => User::paginate(10),
        ]);
    }
}

resources/views/livewire/user-pagination.blade.php

<div>
    <table class="table-auto" style="width: 100%;">
      <thead>
        <tr>
          <th class="px-4 py-2">ID</th>
          <th class="px-4 py-2">Name</th>
          <th class="px-4 py-2">Email</th>
        </tr>
      </thead>
      <tbody>
        @foreach ($users as $user)
            <tr>
            <td class="border px-4 py-2">{{ $user->id }}</td>
            <td class="border px-4 py-2">{{ $user->name }}</td>
            <td class="border px-4 py-2">{{ $user->email }}</td>
            </tr>
        @endforeach
      </tbody>
    </table>
  
    {{ $users->links() }}
</div>
Step 5: Create Routes

Now we will create one route for calling our example, so let's add a new route to the web.php file as bellow:

routes/web.php

Route::get('user-pagination', function () {
    return view('default');
});
Step 6: Create View File

We will create a blade file for the call form route. in this file we will use @livewireStyles, @livewireScripts and @livewire('contact-form'). so let's add it we learn livewire tailwind pagination.

resources/views/default.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How To Create Livewire Pagination Example Laravel 8 - phpcodingstuff.com</title>
    @livewireStyles
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/1.9.0/tailwind.min.css" />
</head>
<body>
    
<div class="container">
    
    <div class="card">
      <div class="card-header">
        How To Create Livewire Pagination Example Laravel 8 - phpcodingstuff.com
      </div>
      <div class="card-body">
        @livewire('user-pagination')
      </div>
    </div>
        
</div>
    
</body>
  
@livewireScripts
  
</html>

Now you can run using the below command:

php artisan serve

I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close