Laravel Vuejs Custom Server Side Pagination Tutorial Example

Throughout this Laravel Vue js custom pagination example tutorial, you will see and understand how to build or create server side pagination in vuejs and laravel application. This example show you step by step guide for make pagination in a Vue.js app with existing API (Server Side pagination) using Axios and bootstrap.

Laravel Vue Js Custom Pagination Example

Follow the following step by step guide on how to make custom server side pagination in laravel and vuejs application.

  • Step 1: Install Laravel App
  • Step 2: Connect Database to App
  • Step 3: Create Api controller
  • Step 4: Updates Routes
  • Step 5: Generate Dummy Records for Testing
  • Step 6: Install Laravel Vue UI
  • Step 7: Initiate Vue in Laravel
  • Step 8: Create Components
  • Step 9: Create Vue Routes
  • Step 10: Define Vue in App.js
  • Step 11: Run your Application

Install Laravel App

Open terminal further run below command to manifest a new laravel project, ignore this step if project is already installed:

composer create-project laravel/laravel laravel-vue-pagination

Connect Database to App

This step explains how to make database connection by adding database name, username and password in .env config file of your project:

DB_DATABASE=#your database name
DB_USERNAME=#database username
DB_PASSWORD=#database password

Create Api controller

You need to create the user controller and define the server side pagination method:

php artisan make:controller Api/UserController

Controller is the quintessential file in Laravel application development. So, without further insert the given below code in UserController.php file.



namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\User;

class UserController extends Controller
    public function index()
        $users = User::orderBy('id', 'desc')->paginate(5);

        return response()->json($users, 200);

Update Routes

Now, we will define API routes. Go to routes/api.php file and declare the foundational code.


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\UserController;

| API Routes
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
Route::get('/users', [UserController::class, 'index']);

Open routes/web.php file, in here; you have to define the following route:



use Illuminate\Support\Facades\Route;

| 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!

Route::get('{any}', function () {
    return view('welcome');
})->where('any', '.*');

Generate Dummy Records for Testing

First run the migration for generating users table in database:

php artisan migrate

Now run the below commands sequentially and generate fake records for testing the laravel vue server side pagination example:

php artisan tinker

You can see in your users table 50 records created.

Install Laravel Vue UI

Run composer command to install Vue UI in laravel, it will manifest vue laravel scaffold:

composer require laravel/ui
php artisan ui vue

After that, use the command to install the vue router and vue axios packages. These packages are used to consume Laravel CRUD APIs.

npm install vue-router vue-axios

Subsequently, execute the command to install npm packages:

npm install

The npm run watch command compiles the assets, not just that with run watch command you don’t fret about re-run the compiler over and over again.

Initiate Vue in Laravel

Now open resources/views/layout/app.blade.php file and put the below code on it.

<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" value="{{ csrf_token() }}" />
    <title>Vue JS CRUD Operations in Laravel</title>
    <link href="{{ mix('css/app.css') }}" type="text/css" rel="stylesheet" />
    <div id="app"></div>
    <script src="{{ mix('js/app.js') }}" type="text/javascript"></script>

Create Components

Next, you have to add the router-view directive in app.vue file; this template will help invoke all the vue routes associated with the components.

So, create app.vue file in resources/js folder, put the below code in resources/js/app.vue file:

    <div class="container">
        <nav class="navbar navbar-expand-lg navbar-light bg-light">
            <div class="collapse navbar-collapse">
                <div class="navbar-nav">
                    <router-link class="nav-item nav-link ml-3" to="/users">Users</router-link>
        <router-view> </router-view>

Now create a global component named as pagination.vue inside your resouces/js/components:

So open resources\js\components\pagination.vue file and put the below code on it:

    <div class="pagination_block">
        <nav aria-label="Page navigation example">
            <ul class="pagination justify-content-center">
                <li class="page-item" :class="{disabled : pagination.prev_page_url == null}">
                    <button type="button" class="page-link" @click="pageURL(pagination.first_page_url)">&laquo;</button>
                <li class="page-item" :class="{disabled : pagination.prev_page_url == null}">
                    <button type="button" class="page-link" @click="pageURL(pagination.prev_page_url)">Previous</button>
                <li class="page-item" v-if="current_page -2 > 0 " >
                    <button type="button" class="page-link" @click="pageURL(pagination.path + '?page=' + (current_page - 2 ) )">{{ current_page - 2}}</button>
                <li class="page-item" v-if="pagination.prev_page_url" >
                    <button type="button" class="page-link" @click="pageURL(pagination.prev_page_url)">{{ current_page - 1}}</button>
                <li class="page-item">
                    <button type="button" class="page-link" :class="{current : pagination.current_page == current_page}">{{ current_page}}</button>
                <li class="page-item" v-if="pagination.next_page_url" >
                    <button type="button" class="page-link" @click="pageURL(pagination.next_page_url)">{{ current_page + 1}}</button>
                <li class="page-item" v-if="current_page + 2 <= last_page" >
                    <button type="button" class="page-link" @click="pageURL(pagination.path + '?page=' + (current_page + 2 ) )">{{ current_page + 2 }}</button>
                <li class="page-item" :class="{disabled : pagination.next_page_url == null}">
                    <button type="button" class="page-link" @click="pageURL(pagination.next_page_url)">Next</button>
                <li class="page-item" :class="{disabled : pagination.next_page_url == null}">
                    <button type="button" class="page-link" @click="pageURL(pagination.last_page_url)">&raquo;</button>
    export default {
        props: ['pagination', 'current_page', 'last_page'],
        methods: {
            pageURL(url) {

This global component work in your hole project common way.

Add code in resources/js/components/users.vue file; in here we are getting all data from database and deleting single product object from database through vue component.

    <div class="container">
        <h1 class="text-center">Users</h1>
        <div class="row">
            <div class="col-md-12">
                <table class="table">
                        <th>Created At</th>
                        <tr v-for="(user, key) of">
                            <td>{{ key+1 }}</td>
                            <td>{{ }}</td>
                            <td>{{ }}</td>

    export default {
        data () {
            return {
                users: {}
        created () {
        methods: {
            pageURL (url) {
                let vm = this;
                vm.loading = true;
                axios.get(url).then(({ data }) => {
                    vm.users = data;
                    vm.loading = false;
                .catch((error) => {
                    vm.loading = false;

Create Vue Routes

In this step, you have to create vue routes, create routes.js inside resources/js directory, add the code in the resources/js/routes.js file:

import Vue from 'vue';
import VueRouter from 'vue-router';

import Users from './components/dashboard/users.vue';
const routes = [
        path: '/users',
        component: Users,
        name: "Users"

 const router = new VueRouter({
     mode: 'history',
     routes: routes

export default router;

Define Vue in App.js

This final step brings you to the point where you must include the required packages in the app.js file. Without further ado, please add the following code in the resources/js/app.js file:

 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.

import Vue from 'vue';

import axios from 'axios';
import VueAxios from 'vue-axios';


import Pagination from './components/pagination.vue'
Vue.component('pagination', Pagination);

Vue.use(VueAxios, axios);

import App from './app.vue';
import router from './routes';

const app = new Vue({
    el: '#app',
    render: h => h(App)

Run your Application

To start the server side pagination app, you need to run the two following commands respectively in two different terminals simultaneously:

npm run watch
php artisan serve

Open the URL in the browser:

Let’s see:

Laravel paginator send the response something like this:

current_page: 1, data: [{id: 51, name: "Vince Beer", email: "",…},…],…}
current_page: 1
data: [{id: 51, name: "Vince Beer", email: "",…},…]
0: {id: 51, name: "Vince Beer", email: "",…}
1: {id: 50, name: "Quentin Schimmel", email: "",…}
2: {id: 49, name: "Mr. Lorenza Gerhold III", email: "",…}
3: {id: 48, name: "Jefferey Witting", email: "",…}
4: {id: 47, name: "Linda Berge", email: "",…}
first_page_url: ""
from: 1
last_page: 11
last_page_url: ""
links: [{url: null, label: "&laquo; Previous", active: false},…]
next_page_url: ""
path: ""
per_page: 5
prev_page_url: null
to: 5
total: 51

I hope you enjoy with vuejs pagination example with laravel tutorial.

Leave a Comment