In this tutorial you will learn about the Laravel 8 Vue JS Flash Message Tutorial and its application with practical example.
In this Laravel 7 Vue JS Flash Message Example Tutorial I’ll show you how to create Flash Message with Vue Js in laravel. In this tutorial you will learn to add Flash Message with Vue Js in laravel. While working with laravel application we come to situations where we want to show/display various flash success and error messages. In this article I will demonstrate you how to display various type of flash messages or notifications in laravel 8.
Laravel 8 Vue JS Flash Message Tutorial
In this step by step tutorial I will demonstrate you how to display Vue Js flash message in laravel 8. Please follow the instruction given below:
- Step 1: Install Laravel 8 App
- Step 2: Connecting App to Database
- Step 3: Create Model And Migration
- Step 4: NPM Configuration
- Step 5: Add Routes
- Step 6: Create Controller By Command
- Step 7: Create Vue Component
- Step 8: Register Vue App
- Step 9: Run Development Server
Step 1: Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
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. Please run the following command:
1 |
php artisan make:model Post -m |
The above command will create a model name post.php and also create a migration file for the posts table. Now open create_postss_table.php migration file from database>migrations and put the following code:
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 |
<?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->increments('id'); $table->string('title'); $table->text('description'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Step 4: NPM Configuration
1 |
php artisan preset vue |
Install all Vue dependencies:
1 |
npm install |
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 3 4 5 6 |
use App\Http\Controllers\PostController; Route::get('post', function () { return view('post'); }); Route::post('store', [PostController::class, 'store']); |
Step 6: Create Controller By Command
Now, lets create a controller named PostController using command given below –
1 |
php artisan make:controller PostController |
Now, go to app\Http\Controllers
and open PostController.php file. Then put the following code into your PostController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; Use App\Models\Post; class PostController extends Controller { public function store(Request $request) { $insert['title'] = $request->get('title'); $insert['description'] = $request->get('description'); $check = Post::insertGetId($insert); return response()->json($check); } } |
Step 7: Create Vue Component
In this step we will create a vue component. Go to resources/assets/js/components
folder and create a file called Post.vue. Then put the following code into your Post.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 52 53 54 55 56 57 58 59 |
<template> <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header"> Laravel Vue js Flash Message Example </div> <div class="card-body"> <form @submit="formStore"> <strong> Title:</strong> <input type="text" class="form-control" v-model="title"> <strong> Description:</strong> <textarea class="form-control" v-model="description"> </textarea> <button class="btn btn-success"> Submit</button> </form> <strong> Output:</strong> <pre> {{output}} </pre> </div> </div> </div> </div> </div> </template> <script> export default { mounted() { console.log('Component mounted.') }, data() { return { title: '', description: '', output: '' }; }, methods: { formStore(e) { e.preventDefault(); let currentObj = this; axios.post('/store', { title: this.title, description: this.description }) .then(function (response) { currentObj.output = response.data; flash('Post Created Successfully', 'success'); }) .catch(function (error) { currentObj.output = error; }); } } } </script> |
Now, create a new components named flash.vue and update the following code into flash.vue 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 |
<template> <div class="alert alert-success spacing" role="alert" v-show="show"> {{ body }} </div> </template> <script> export default { props: ['message'], data() { return { show : false, body : '' } }, created() { if(this.message) { this.flash(this.message) } window.events.$on('flash',(message) => this.flash(message)) }, methods: { flash(message) { this.show = true this.body = message setTimeout(() => { this.hide() },3000) }, hide() { this.show = false } } } </script> <style> .spacing { position: fixed; right: 25px; bottom: 25px; } </style> |
Now open
resources/assets/js/app.js
and include the Post.vue and Flash.vue component as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
require('./bootstrap'); window.Vue = require('vue'); window.events = new Vue(); window.flash = function(message) { window.events.$emit('flash',message); } Vue.component('post-component', require('./components/Post.vue')); Vue.component('flash', require('./components/Flash.vue')); const app = new Vue({ el: '#app' }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
require('./bootstrap'); window.Vue = require('vue'); window.events = new Vue(); window.flash = function(message) { window.events.$emit('flash',message); } Vue.component('post-component', require('./components/Post.vue')); Vue.component('flash', require('./components/Flash.vue')); const app = new Vue({ el: '#app' }); |
Step 8: Register Vue App
In this step, you need to create a blade view file to define Vue’s app. Go to resources/views
folder and make a file named post.blade.php. Then update the below code into post.blade.php file:
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="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 8 Vue Flash Message Example - w3adda.com</title> <link href="{{asset('css/app.css')}}" rel="stylesheet" type="text/css"> </head> <body> <div id="app"> <flash message=""></flash> <post-component></post-component> </div> <script src="{{asset('js/app.js')}}" ></script> </body> </html> |
Step 9: Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 2 3 4 5 |
npm run dev or npm run watch |