In this tutorial you will learn about the Laravel 7 Vue JS Datatables Example Tutorial and its application with practical example.
In this Laravel 7 Vue JS Datatables Example Tutorial I’ll show you how to use Datatables with Vue Js in laravel. In this tutorial you will learn to use datatables with Vue Js in laravel.
Laravel 7 Vue JS Datatables Example Tutorial
- Download Laravel Fresh App
- Add Database Detail
- Create Model And Migration
- NPM Configuration
- Add Routes
- Create Controller By Command
- Create Vue Component
- Create Blade Views And Initialize Vue Components
- Run Development Server
Step 1: Download Laravel Fresh App
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Add Database Details
1 2 3 4 5 6 |
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: Create Model And Migration
Now, in this step we will create model and migration file for form. Please run the following command:
1 |
php artisan make:model Post -fm |
This command will create one model name post.php and also create one migration file for the posts table. Now open create_posts_table.php migration file and put the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('title'); $table->string('slug'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Next, Navigate to app/Post.php and update the following code into your Post.php model as follow:
1 2 3 4 5 6 7 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $guarded = []; } |
Next, Navigate to database/factories and open PostFactory.php. Then update the following code into it as follow:
1 2 3 4 5 6 7 8 9 |
/** @var \Illuminate\Database\Eloquent\Factory $factory */ use App\Post; use Faker\Generator as Faker; $factory->define(Post::class, function (Faker $faker) { return [ 'title' => $faker->sentence, 'slug' => \Str::slug($faker->sentence) ]; }); |
now run the following command to generate fake data using faker as follow:
1 2 3 4 |
php artisan tinker //and then factory(\App\Post::class,100)->create() exit |
Step 4: NPM Configuration
You need to setup Vue and install Vue dependencies using NPM. So run the following command on command prompt:
1 |
php artisan preset vue |
Install all Vue dependencies:
1 |
npm install |
Install vuejs-datatable:
1 |
npm install vuejs-datatable |
Step 5: Add Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 |
//all posts Route::get('posts','PostController@index'); |
Step 6: Create Controller By Command
Now, lets create a controller named PostController using command given below –
1 |
php artisan make:controller PostController |
After that, go to app\Http\Controllers and open PostController.php file. Then update the following code into your PostController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
namespace App\Http\Controllers; use App\Post; use Illuminate\Http\Request; use Facades\App\Repository\Posts; class PostController extends Controller { public function index(Request $request) { $posts = Post::get(); return response()->json($posts); } } |
Step 7: Create Vue Component
Next step, go to resources/assets/js/components folder and create a file called DataTableComponent.vue.
Now, update the following code into your DataTableComponent.vue components file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<template> <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Laravel Vue Datatables Component Example</div> <div class="card-body"> <datatable :columns="columns" :data="rows"></datatable> <datatable-pager v-model="page" type="abbreviated" :per-page="per_page"></datatable-pager> </div> </div> </div> </div> </div> </template> <script> import DatatableFactory from 'vuejs-datatable'; export default { components: { DatatableFactory }, mounted() { console.log('Component mounted.') }, data(){ return { columns: [ {label: 'id', field: 'id'}, {label: 'Title', field: 'title'}, {label: 'Slug', field: 'slug'} ], rows: [], page: 1, per_page: 10, } }, methods:{ getPosts: function() { axios.get('/posts').then(function(response){ this.rows = response.data; }.bind(this)); } }, created: function(){ this.getPosts() } } </script> |
Now open resources/assets/js/app.js and include the DataTableComponent.vue component as follow:
1 2 3 4 5 6 |
require('./bootstrap'); window.Vue = require('vue'); Vue.component('datatable-component', require('./components/DataTableComponent.vue').default); const app = new Vue({ el: '#app', }); |
Step 8: Create Blade Views And Initialize Vue Components
In this step, navigate to resources/views and create one folder named layouts. Inside this folder create one blade view file named app.blade.php file.
Next, Navigate to resources/views/layouts and open app.blade.php file. Then update the following code into your app.blade.php file as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>Laravel Vue JS DataTable Example Tutorial</title> <script src="{{ asset('js/app.js') }}" defer></script> <link href="{{ asset('css/app.css') }}" rel="stylesheet"> @stack('fontawesome') </head> <body> <div id="app"> <main class="py-4"> @yield('content') </main> </div> </body> </html> |
Now, Navigate to resources/views/ and open welocome.blade.php. Then update the following code into your welcome.blade.php file as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Laravel Vue Js DataTable Example</div> <div class="card-body"> <datatable-component></datatable-component> </div> </div> </div> </div> </div> @endsection |
Step 9: Run Development Server
1 2 3 |
npm run dev or npm run watch |