Laravel Livewire Pagination Example

  61 views   1 month ago Laravel

This post is facile to how to utilize 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 engender simple pagination utilizing laravel livewire utilizing PHP artisan livewire. you can utilize 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 utilizing livewire with laravel then you don't worry about inscribing any code like jquery ajax etc, livewire will avail to inscribe very simple way jquery ajax code utilizing 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.

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

install the livewire running by the following command.

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');
});<pre></pre>

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>Livewire Pagination - HackTheStuff</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">
        Livewire Pagination - HackTheStuff
      </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 you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]