Livewire Auto Load More On Page Scroll in Laravel 10

Want to create an auto load more page scroll functionality with livewire in Laravel 10 project. then this tutorial is for you. So, In this tutorial, you will learn how to create auto load more data on page scroll in Laravel 10 with livewire.

Livewire Auto Load More On Page Scroll in Laravel 10

By following this tutorial step, you can create an auto load more data feature on page scroll using Livewire in Laravel 10.

  • Step 1: Setup New Laravel 10 Project
  • Step 2: Configure Database to Laravel Project
  • Step 3: Run Migration and Add Dummy Data
  • Step 4: Install Livewire Package
  • Step 5: Create Livewire Auto Load More Component
  • Step 6: Add Routes
  • Step 7: Create Blade View File
  • Step 8: Run Development Server

Step 1: Setup New Laravel 10 Project

Firstly, Open your terminal OR command prompt.

Then you need to execute the following command into it to install laravel fresh app in your server:

 composer create-project --prefer-dist laravel/laravel blog 

Step 2: Configure Database to Laravel Project

Once you have installed Laravel app on your server. Now, you need to configure the database with Laravel apps.

So open your project root directory and find .env file. Then add database detail in .env file; as follows:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=here your database name here
DB_USERNAME=here database username here
DB_PASSWORD=here database password here

Step 3: Run Migration and Add Dummy Data

Again, open your terminal or command prompt and execute the following command to create the table into your database:

php artisan migrate

This command will create table into your database.

After that, open your terminal and run the following command to generate fake data using laravel faker as follow:

php artisan tinker

Then

User::factory()->count(100)->create()

Step 4: Install Livewire Package

In this step, you need to install livewire package to your laravel project using the following command:

composer require livewire/livewire

Step 5: Create Livewire Auto Load More Component

In this step, create the livewire components for load more data using the following command. So execute the following command on terminal or command line:

php artisan make:livewire load-more-user-list

This command will create the following components on the following path:

app/Http/Livewire/LoadMoreUserList.php
resources/views/livewire/load-more-user-list.blade.php

Now, Navigate to app/Http/Livewire folder and open LoadMoreUserList.php file. Then add the following code into your LoadMoreUserList.php file:

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\User;

class LoadMoreUserList extends Component
{
    public $perPage = 12;
    protected $listeners = [
        'load-more' => 'loadMore'
    ];

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function loadMore()
    {
        $this->perPage = $this->perPage + 5;
    }

    /**
     * Write code on Method
     *
     * @return response()
     */
    public function render()
    {
        $users = User::latest()->paginate($this->perPage);
        $this->emit('userStore');

        return view('livewire.load-more-user-list', ['users' => $users]);
    }
}

After that, Navigate to resources/views/livewire folder and open load-more-user-list.blade.php file. Then add the following code into your load-more-user-list.blade.php file:

<div>
    <div class="table-responsive">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>No.</th>
                    <th>Name</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                @foreach($users as $user)
                    <tr>
                        <td>{{ $user->id }}</td>
                        <td>{{ $user->name }}</td>
                        <td>{{ $user->email }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    </div>
</div>

Step 6: Add Routes

In this step, Navigate to routes folder and open web.php. Then add the following routes into your web.php file:

Route::get('/load-more-scroll', function () {
    return view('lists');
});

Step 7: Create View File

In this step, navigate to resources/views/folder and create one blade view files that name lists.blade.php file. Then add the following code into your lists.blade.php file:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Laravel 10 Livewire Load More Data on Page Scroll -Tutsmake.com</title>

        <!-- Fonts -->
        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">

        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap.min.css">

        <!-- Styles -->
        <style>
            html, body {
                background-color: #fff;
                color: #636b6f;
                font-family: 'Nunito', sans-serif;
                font-weight: 200;
                height: 100vh;
                margin: 0;
            }

            .full-height {
                height: 100vh;
            }

            .flex-center {
                align-items: center;
                display: flex;
                justify-content: center;
            }

            .position-ref {
                position: relative;
            }

            .top-right {
                position: absolute;
                right: 10px;
                top: 18px;
            }

            .content {
                text-align: center;
            }

            .title {
                font-size: 84px;
            }

            .links > a {
                color: #636b6f;
                padding: 0 25px;
                font-size: 13px;
                font-weight: 600;
                letter-spacing: .1rem;
                text-decoration: none;
                text-transform: uppercase;
            }

            .m-b-md {
                margin-bottom: 30px;
            }
        </style>
    </head>
<body>
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h2>Laravel 10 Livewire Load More Data on Page Scroll -Tutsmake.com</h2>
                    </div>
                    <div class="card-body">
                        @if (session()->has('message'))
                            <div class="alert alert-success">
                                {{ session('message') }}
                            </div>
                        @endif
                        @livewire('load-more-user-list')
                    </div>
                </div>
            </div>
        </div>
    </div>
    @livewireScripts
    <script type="text/javascript">
        window.onscroll = function(ev) {
            if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
                window.livewire.emit('load-more');
            }
        };
   </script>
</body>
</html>
Note that, if you want to add HTML(blade views), CSS, and script code into your livewire files. So, you can use @livewireStyles, @livewireScripts, and @livewire(‘ blade views’).

Step 8: Run Development Server

Finally, you need to run the following PHP artisan serve command to start your laravel livewire load more data on page scroll app:

php artisan serve

If you want to run the project diffrent port so use this below command

php artisan serve --port=8080

Now, you are ready to run laravel livewire load more on page scroll app. So open your browser and hit the following URL into your browser:

http://localhost:8000/load-more-scroll

Recommended Laravel Posts

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *