onlinecode

Laravel Vue JS Axios Post Request Example and Demo

Laravel Vue JS Axios Post Request Example and Demo

In this post we will give you information about Laravel Vue JS Axios Post Request Example and Demo. Hear we will give you detail about Laravel Vue JS Axios Post Request Example and DemoAnd how to use it also give you demo for it if it is necessary.

Are you new with Laravel Vue and Axios?, If yes then you are the right place. In this tutorial, I will share with you how to send POST request form data using axios with vue js in Laravel 5.6. You will learn to send post form input data with csrf token and you can access input data in controller method.

If you are learning or working with vue js or angular js project then you should learn and use axios package for http request of GET, POST, DELETE and PUT. In this example we will create one basic example of form and with input data, when user submit form then all data will be send to laravel controller method. we will also pass csrf token on our post method. we will create controller method and return request data and then simply print on front-end side so you can see output.

We will create step by step form and then submit form using vue js and axios. you can see following screen shot. You can also download code and check demo too.

Preview:

Step 1 : Install Laravel Fresh App

Here, we will 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 Route

In this step, we will create one post route and return all post form data. So, let’s add new route on that file.

routes/web.php

Route::post('formSubmit','PostController@formSubmit');

Also see:Laravel 5 and Vue JS CRUD with Pagination example and demo from scratch

Step 3: Create New Controller

in this step, now we have create PostController with formSubmit methods, in this method we will return request data. So let’s create controller:

app/Http/Controllers/PostController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

class PostController extends Controller

{

/**

* success response method.

*

* @return IlluminateHttpResponse

*/

public function formSubmit(Request $request)

{

return response()->json([$request->all()]);

}

}


Step 4: NPM Configuration

In this step, we have to first add setup of vue js and then install npm, so let’s run bellow command in your project.

Install vue:

php artisan preset vue

Install npm:

npm install

Step 5: Write 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.component('example-component', require('./components/ExampleComponent.vue'));

const app = new Vue({

el: '#app'

});

resources/assets/js/components/ExampleComponent.vue

<template>

<div >

<div >

<div >

<div >

<div >Laravel Vue Axios Post - ItSolutionStuff.com</div>

<div >

<form @submit="formSubmit">

<strong>Name:</strong>

<input type="text" v-model="name">

<strong>Description:</strong>

<textarea v-model="description"></textarea>

<button >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 {

name: '',

description: '',

output: ''

};

},

methods: {

formSubmit(e) {

e.preventDefault();

let currentObj = this;

axios.post('/formSubmit', {

name: this.name,

description: this.description

})

.then(function (response) {

currentObj.output = response.data;

})

.catch(function (error) {

currentObj.output = error;

});

}

}

}

</script>

Step 6: 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 axios post - ItSolutionStuff.com</title>

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

</head>

<body>

<div id="app">

<example-component></example-component>

</div>

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

</body>

</html>

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

Also see:Laravel Vue JS Infinite Scroll Example with Demo

npm run dev

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

I hope it can help you…

Hope this code and post will helped you for implement Laravel Vue JS Axios Post Request Example and Demo. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Exit mobile version