Laravel Vue JS Pagination Example with Demo

By Hardik Savani April 16, 2024 Category : PHP Laravel Vue.JS

Today, i would like to share with you how to create laravel vue pagination from scratch. We will create dynamic pagination with vue.js. We will use laravel-vue-pagination npm package for vue pagination in laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

As we know in todays, laravel become more popular and as well as vue js too. Almost like to develop their website or project in Laravel with vue js. We all love vue js because it's really smooth and nice to work with it. So, in this post, we will see how to create pagination using vue and laravel.

In this example, i simply download laravel 5.6 fresh project and then create "categories" table with model. Then i installed vue and npm install, then i also installed vue-resource because we need to use http request. Then i installed laravel-vue-pagination package, after that we can run over application.

You have to just follow step by step this tutorial and you will get very nice example and very easily. You can also download and see demo as bellow link. You will have layout like as bellow preview:

Preview:

Step 1 : Install Laravel 5.6 App

we are going to from scratch so, we need to get fresh Laravel 5.6 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 Category Table and Model

next step, we need to create migration for "categories" table using Laravel 5.6 php artisan command, so first fire bellow command:

php artisan make:migration create_categories_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create categories table.

<?php


use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;


class CreateCategoriesTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('categories', function (Blueprint $table) {

$table->increments('id');

$table->string('name');

$table->timestamps();

});

}


/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('categories');

}

}

After create migration we need to run above migration by following command:

php artisan migrate

After create "categories" table you should create Category model for categories, so first create file in this path app/Category.php and put bellow content in Category.php file:

app/Category.php

<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Category extends Model

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

protected $fillable = [

'name'

];

}

After create migration and model, Make sure you have to add some dummy records in your "categories" table.

Step 3: Create Route

In this step, we will create one route for getting data. So, let's add new route on that file.

routes/web.php

Route::get('categories', 'CategoryController@index');

Step 4: Create Controller

in this step, now we have create CategoryController with index methods, in this method we will return pagination data. So let's create controller:

app/Http/Controllers/CategoryController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Category;

class CategoryController extends Controller

{

/**

* success response method.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$data = Category::paginate(10);

return response()->json($data);

}

}

Step 5: NPM Configuration

In this step, we have to first add setup of vue js and then install npm, after that we will install vue-resource and laravel-vue-pagination, so let's run bellow command in your project.

Install vue:

php artisan preset vue

Install npm:

npm install

Install vue-resource:

npm install vue-resource

Install laravel-vue-pagination:

npm install laravel-vue-pagination

Step 6: Work on app.js and Components

Here, we will write code on app.js and then we will create vue js components, So let's create both file and put bellow code:

resources/assets/js/app.js

require('./bootstrap');

window.Vue = require('vue');

Vue.use(require('vue-resource'));

Vue.component('data-component', require('./components/DataComponent.vue'));

Vue.component('pagination', require('laravel-vue-pagination'));

const app = new Vue({

el: '#app'

});

resources/assets/js/components/DataComponent.vue

<template>

<div class="container">

<div class="row justify-content-center">

<div class="col-md-8">

<div class="card">

<div class="card-header">Laravel vue pagination - ItSolutionStuff.com</div>

<div class="card-body">

<ul>

<li v-for="tag in laravelData.data" :key="tag.id">{{ tag.name }}</li>

</ul>

<pagination :data="laravelData" @pagination-change-page="getResults"></pagination>

</div>

</div>

</div>

</div>

</div>

</template>

<script>

export default {

mounted() {

console.log('Component mounted.')

},

data() {

return {

laravelData: {},

}

},

created() {

this.getResults();

},

methods: {

getResults(page) {

if (typeof page === 'undefined') {

page = 1;

}

this.$http.get('/categories?page=' + page)

.then(response => {

return response.json();

}).then(data => {

this.laravelData = data;

});

}

}

}

</script>

Step 7: Update welcome.blade.php

At last step, we will update our welcome.blade.php file. in this file we will use app.js file and use it, so let's update.

resources/views/welcome.blade.php

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="csrf-token" content="{{ csrf_token() }}">

<meta http-equiv="X-UA-Compatible" content="IE=edge">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Laravel 5.6 Vue JS Pagination</title>

<link href="{{asset('css/app.css')}}" rel="stylesheet" type="text/css">

</head>

<body>

<div id="app">

<data-component></data-component>

</div>

<script src="{{asset('js/app.js')}}" ></script>

</body>

</html>

Now you have to run below command for update app.js file:

npm run dev

Now you can check our example and also check demo and download free code.

I hope it can help you...

Shares