Laravel 7 Vue JS File Upload Example

Laravel 7 Vue JS File Upload Example

Laravel Vue js File Upload Example tutorial. In this tutorial, you will learn how to upload files, images and documents with vue js components in laravel.

As well as you will learn how to send form data (files, images, input fields etc) with Vue js using Axios to send POST request in laravel.

This tutorial will guide you step by step for upload files with Vue js using Axios post request in laravel.

Laravel Vue JS File Upload Example with Axios

Follow the following steps and upload files into database and webserver folder using vue js in laravel:

  • Step 1: Download Laravel Fresh App
  • Step 2: Add Database Detail
  • 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: Download Laravel Fresh App

In this step, you need to install laravel latest application setup, So open your terminal OR command prompt and run the following command:

 composer create-project --prefer-dist laravel/laravel blog 

Step 2: Add Database Details

After successfully install laravel Application, Go to your project .env file and set up database credential and move next step:

 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

Next step, you need to run the following command:

php artisan make:model Photo -m

This command will create one model name photo.php and also create one migration file for the photos table.

Now open create_photos_table.php migration file from database>migrations and replace up() function with following code:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePhotosTable extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('photos', function (Blueprint $table) {
     * Reverse the migrations.
     * @return void
    public function down()

Next, migrate the table using the below command:

php artisan migrate

Step 4: NPM Configuration

You need to setup Vue and install Vue dependencies using NPM. So run the following command on command prompt:

php artisan preset vue

Install all Vue dependencies:

npm install

Step 5: Add Routes

Next step, go to routes folder and open web.php file and add the following routes into your file:


Route::get('upload_file', function () {
    return view('upload');
Route::post('store_file', '[email protected]');

Step 6: Create Controller By Command

Next step, open your command prompt and run the following command to create a controller by an artisan:

php artisan make:controller FileUploadController

After that, go to app\Http\Controllers and open FileUploadController.php file. Then update the following code into your FileUploadController.php file:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
Use App\Photo;
class FileUploadController extends Controller
    // function to store file in 'upload' folder
    public function fileStore(Request $request)
        $upload_path = public_path('upload');
        $file_name = $request->file->getClientOriginalName();
        $generated_new_name = time() . '.' . $request->file->getClientOriginalExtension();
        $request->file->move($upload_path, $generated_new_name);
        $insert['title'] = $file_name;
        $check = Photo::insertGetId($insert);
        return response()->json(['success' => 'You have successfully uploaded "' . $file_name . '"']);

Step 7: Create Vue Component

Next step, go to resources/assets/js/components folder and create a filed called FileUpload.vue.

And update the following code into your FileUpload.vue components file:

<div class="container" style="margin-top: 50px;">
<div class="text-center">
<h4>File Upload with VueJS and Laravel</h4><br>
<div style="max-width: 500px; margin: 0 auto;">
<div v-if="success !== ''" class="alert alert-success" role="alert">
<form @submit="submitForm" enctype="multipart/form-data">
<div class="input-group">
<div class="custom-file">
<input type="file" name="filename" class="custom-file-input" id="inputFileUpload"
<label class="custom-file-label" for="inputFileUpload">Choose file</label>
<div class="input-group-append">
<input type="submit" class="btn btn-primary" value="Upload">
<p class="text-danger font-weight-bold">{{filename}}</p>
export default {
mounted() {
console.log('Component successfully mounted.')
data() {
return {
filename: '',
file: '',
success: ''
methods: {
onFileChange(e) {
this.filename = "Selected File: " +[0].name;
this.file =[0];
submitForm(e) {
let currentObj = this;
const config = {
headers: {
'content-type': 'multipart/form-data',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
// form data
let formData = new FormData();
formData.append('file', this.file);
// send upload request'/store_file', formData, config)
.then(function (response) {
currentObj.success =;
currentObj.filename = "";
.catch(function (error) {
currentObj.output = error;

Now open resources/assets/js/app.js and include the FileUpload.vue component like this:app.js

window.Vue = require('vue');
Vue.component('file-upload-component', require('./components/FileUpload.vue').default);
const app = new Vue({
el: '#app',

Step 8: Register Vue App

In this step, we will create a blade view file to define Vue’s app. Go to resources/views folder and make a file named upload.blade.php. Then update the following code into upload.blade.php as follow:

<!doctype html>
<html lang="en">
<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, shrink-to-fit=no">
<link rel="stylesheet" href="">
<title>File Upload with VueJS and Laravel -</title>
<link href="{{asset('css/app.css')}}" rel="stylesheet" type="text/css">
<div id="app">
<script src="{{asset('js/app.js')}}"></script>

Step 9: Run Development Server

Run the following command to start development server:

npm run dev
npm run watch


In this example tutorial, you have learned how to upload files with vue js in laravel using axios request.

Recommended Laravel Vue js Posts


My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *