In this tutorial you will learn about the Laravel 7 Vue JS Post Axios Request Example and its application with practical example.
In this Laravel 7 Vue JS Axios post Request Example tutorial, I’ll show you how to implement post request with Vue JS Axios in laravel. In this tutorial you will learn to create post request with Vue JS Axios in laravel.
Laravel 7 Vue JS Post Axios Request Example
- Step 1: Download Laravel Fresh Setup
- Step 2: Setup Database Credentials
- Step 3: Make Migration & Model
- Step 4: Add Routes
- Step 5: Make Controller By Command
- Step 6: Install Vue Js dependency
- Step 7: Create blade file and layout
- Step 8: Run Development Server
Step 1: Download Laravel Fresh Setup
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Setup Database Credentials
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: Make Migration & Model
Now, in this step we will create model and migration file for form. Please run the following command:
1 |
php artisan make:model Post -m |
after that, open your posts migration file and paste the following code:
1 2 3 4 5 |
Schema::create('posts', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('title'); $table->timestamps(); }); |
Again open your terminal and run the php artisan migrate to migrate our tables into your database:
1 |
php artisan migrate |
After that, open Post model and update the following code into your app/Post.php model file:
1 2 3 4 5 6 7 |
namespace App; use App\Events\PostCreated; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $guarded = []; } |
Step 4: 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 |
Route::get('/post', 'PostController@index')->name('post'); Route::post('/post', 'PostController@createPost'); |
Step 5: Make Controller By Command
Now, lets create a controller named PostController using command given below –
1 |
php artisan make:controller PostController |
This command will create a controller that name PostController inside the controller folder. Next, open it app/Https/Controller/PostController.php and update the following methods into your PostController 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 App\Post; use Illuminate\Http\Request; class PostController extends Controller { public function index() { return view('post'); } public function createPost(Request $request) { $post = new Post; $post->title = $request->title; $post->save(); return response()->json([ 'message' => 'New post created' ]); } } |
Step 6: Install Vue Js dependency
1 |
npm install |
now open webpack.mix.js file and update the following code into your file. Make an asstes folder inside resources folder and copy js and sass folder inside it. Thats all. We need it to setup our laravel mix
1 2 3 |
const mix = require('laravel-mix'); mix.js('resources/assets/js/app.js', 'public/js/app.js') .sass('resources/assets/sass/app.scss', 'public/css/app.css'); |
Next Go to resources/assets/js/components/ folder. And create a new components name PostComponent.vue. Then update the following code into your PostComponent.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 |
<template> <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-header">Create new post</div> <div class="card-body"> <p id="success"></p> <form @submit.prevent="addNewPost"> <input type="text" name="title" v-model="newPost"> <input type="submit" value="Submit"> </form> </div> </div> </div> </div> </div> </template> <script> export default { data(){ return { newPost: '' } }, methods: { addNewPost(){ axios.post('/post',{title: this.newPost, user_id:userId}) .then((response)=>{ $('#success').html(response.data.message) }) } } } </script> |
The above code is to send form data to controller using axios post request in laravel. Next, Go to resources/assets/js then open app.js file and intialize vue js components in this file. So open app.js file and update the following code into your app.js file:
1 2 3 4 5 6 |
require('./bootstrap'); window.Vue = require('vue'); Vue.component('post-component', require('./components/PostComponent.vue').default); const app = new Vue({ el: '#app', }); |
Step 7: Create blade file and layout
Now, Open resources/layouts/app.blade.php and update the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<!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>Send form data with vue js in laravel</title> </head> <body> <div id="app"> <div class="py-4"> @yield('content') </div> </div> <script src="{{ mix('js/app.js') }}" defer></script> </body> </html> |
Next, resources/views/ and create a new blade view file name post.blade.php. And update the following code into your post.blade.php view file:
1 2 3 4 5 |
@extends('layouts.app') @section('content') <post-component></post-component> @endsection |
Step 8: Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 2 3 |
npm run dev //or npm run watch |
Now, open the following URL in browser to see the output –
1 |
http://localhost:8000/post |