In this article, we will share with you how to create infinite scroll pagination using vue js with laravel 8(Laravel 8 Vue JS Infinite Scroll Load More Example).
sometimes we need to create a mouse scroll or infinite scroll pagination, like Facebook messenger. we give you a simple example of vuejs infinite loading data using laravel.
In this example, we are creating a simple example of Vue JS and laravel. so you can see the following steps.
Overview
Step 1: Install Laravel 8
Step 2: Setting Database Configuration
Step 3: Create Routes
Step 4: Create Controller
Step 5: Install Vue Js dependency
Step 6: Create Component and update app.js
Step 7: Update Blade Files
Step 8: Run Our Laravel Application
Step 1: Install Laravel 8
We are going to install laravel 8, so first open the command prompt or terminal and go to xampp htdocs folder directory using the command prompt. after then run the below command for laravel 8 install.
composer create-project --prefer-dist laravel/laravel laravel8_vue
Step 2: Setting Database Configuration
After the complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name(laravel8_vue) DB_USERNAME=Enter_Your_Database_Username(root) DB_PASSWORD=Enter_Your_Database_Password(root)
Step 3: Create Routes
Add the following route code in the “routes/web.php” file.
use App\Http\Controllers\UserController; Route::get('/users', 'UserController@index');
Step 4: Create Controller
Here below command help to create the controller and model.
php artisan make:controller UserController
UserController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\Response; use App\Models\User; use Illuminate\Support\Facades\Hash; class UserController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $data = User::orderBy('id')->paginate(10); return response()->json($data); } } ?>
Step 5: Install Vue Js dependency
in this step, go to the project directory using the command prompt. after then run the below command for Install Vue Js dependency.
npm install
Install vue-resource
npm install vue-resource
Install vue-infinite-loading
npm install vue-infinite-loading
Step 6: Create Component and update app.js
resources/js/components/UserComponent.vue
<template> <div class="container" style="margin-top:50px;"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header"><strong>User List</strong></div> <div class="card-body"> <div> <p v-for="item in list" :key="item.id"> <span>{{item.name}}</span> <span>{{item.email}}</span> </p> <infinite-loading @distance="1" @infinite="infiniteHandler"></infinite-loading> </div> </div> </div> </div> </div> </div> </template> <script> export default { data() { return { list: [], page: 1, }; }, methods: { infiniteHandler($state) { this.$http.get('/users?page='+this.page) .then(response => { return response.json(); }).then(data => { $.each(data.data, (key, value)=> { this.list.push(value); }); $state.loaded(); }); this.page = this.page + 1; }, }, } </script>
resources/js/app.js
require('./bootstrap'); window.Vue = require('vue'); Vue.use(require('vue-resource')); Vue.component('user-component', require('./components/UserComponent.vue').default); Vue.component('InfiniteLoading', require('vue-infinite-loading')); const app = new Vue({ el: '#app', });
Step 7: Update Blade Files
So finally, first we need to create the app.blade.php file in the “resources/views/layouts/” and Update the welcome.blade.php file in the “resources/views/” directory. so you can see the below code.
resources/views/layouts/app.blade.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>{{ config('app.name', 'Laravel') }}</title> <link href="{{ mix('css/app.css') }}" rel="stylesheet"> <title>User</title> </head> <body> <div id="app"> <div class="py-4"> @yield('content') </div> </div> <script src="{{ mix('js/app.js') }}" defer></script> </body> </html>
welcome.blade.php
@extends('layouts.app') @section('content') <user-component></user-component> @endsection
Step 8: Run Our Laravel Application
We can start the server and run this example using the below command.
npm run dev //or npm run watch
Now we will run our example using the below Url in the browser.
http://127.0.0.1:8000/user