In this laravel vue js Axios post request example, you will learn how to send form data with Vue js using axios post http request in laravel.
This tutorial will guide you step by step on how to send form data to the controller using axios post HTTP request in laravel with Vue js.
Laravel Vue JS Axios Post Request Example Tutorial
Just follow the following steps and make Axios HTTP post request in laravel with vue js and submit form data to the controller:
- 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
Use the following command to install the laravel fresh setup. So open your terminal OR command prompt and run following command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Setup Database Credentials
After successfully download laravel Application, Go to your project root directory and open .env file. Then set up database credential as follow:
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
Use the following command to create a migration file and model for our post table:
php artisan make:model Post -m
after that, open your posts migration file and paste the following code:
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:
php artisan migrate
After that, open Post model and update the following code into your app/Post.php model file:
namespace App; use App\Events\PostCreated; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $guarded = []; }
Step 4: Add Routes
Add the following routes into your route file. So go app/routes/web.php and update the following routes:
routes/web.php
Route::get('/post', 'PostController@index')->name('post'); Route::post('/post', 'PostController@createPost');
Step 5: Make Controller By Command
Now open your terminal and run the following command:
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:
<?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
Now, go inside the project folder and install the frontend dependencies using the following command.
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.
webpack.mix.js
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:
<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:
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:
<!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:
@extends('layouts.app') @section('content') <post-component></post-component> @endsection
Step 8: Run Development Server
Now everything is set to go. Now just we have to compile our all javascript file. so run below command.
npm run dev //or npm run watch
Now you are ready to run this app, so hit the below URL into your browser:
http://localhost:8000/post
Conclusion
In this laravel vue js axios post request example, you have learned how to send form data with parameters uisng axios post request with vue js in laravel.