Vue js Laravel 5.8 File Upload Tutorial

Today, We want to share with you Vue js Laravel 5.8 File Upload Tutorial.In this post we will show you File upload with Vue and Laravel 5.8, hear for Laravel Vue JS Image Upload Example with Demo we will give you demo and example for implement.In this post, we will learn about Image upload and validation using Laravel and VueJs with an example.

Vue js Laravel 5.8 File Upload Tutorial

There are the Following The simple About Vue js Laravel 5.8 File Upload Tutorial Full Information With Example and source code.

As I will cover this Post with live Working example to develop Upload files with Vue.Js and Laravel 5.8, so the Image upload and validation using Laravel and VueJs for this example is following below.

Step 1 : Install Laravel 5.8

install Laravel 5.8 mainlication using bellow command

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

Step 2: Add Route

routes/web.php

Route::post('productInsert','[email protected]');

Step 3: Define a Laravel 5.8 Controller

main/Http/Controllers/uploadProductImgController.php

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
 
class uploadProductImgController extends Controller
{
    public function productInsert(Request $request)
    {
    	$imageName = time().'.'.$request->image->getClientOriginalExtension();
        $request->image->move(public_path('images'), $imageName);
        
    	return response()->json(['success'=>'You have successfully upload image.']);
    }
}

Step 4:Define NPM Configuration

Install vue:

php artisan preset vue

simple Install npm:

npm install

Step 5: Vue JS and Components Code

resources/assets/js/main.js

require('./bootstrap');
 
window.Vue = require('vue');
 
Vue.component('product-component', require('./components/ProductComponent.vue'));
 
const main = new Vue({
    el: '#main'
});

resources/assets/js/components/ProductComponent.vue

<template>
    <div class="dsp container">
        <div class="row product-upload justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Laravel 5.8 with Vue JS Image Upload - pakainfo.com</div>
 
                    <div class="card-body">
                        <div v-if="success != ''" class="alert alert-success" role="alert">
                          {{success}}
                        </div>
                        <form @submit="productInsert" enctype="multipart/form-data">
                        <strong>Name:</strong>
                        <input type="text" class="dsp form-control" v-model="name">
                        <strong>Product Image:</strong>
                        <input type="file" class="dsp form-control" v-on:change="onImageChange">
 
                        <button class="btn btn-success">Submit</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
 
<script>
    export default {
        mounted() {
            console.log('Your Component mounted.')
        },
        data() {
            return {
              name: '',
              image: '',
              success: ''
            };
        },
        methods: {
            onImageChange(e){
                console.log(e.target.files[0]);
                this.image = e.target.files[0];
            },
            productInsert(e) {
                e.preventDefault();
                let productObject = this;
 
                const config = {
                    headers: { 'content-type': 'multipart/form-data' }
                }
 
                let formData = new FormData();
                formData.mainend('image', this.image);
 
                axios.post('/productInsert', formData, config)
                .then(function (response) {
                    productObject.success = response.data.success;
                })
                .catch(function (error) {
                    productObject.output = error;
                });
            }
        }
    }
</script>

Step 6: Update product_list.blade.php

resources/views/product_list.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.8 with Vue JS Image Upload - pakainfo.com</title>
        <link href="{{asset('css/main.css')}}" rel="stylesheet" type="text/css">
    </head>
    <body>
        <div id="main">
            <product-component></product-component>
        </div>
        <script src="{{asset('js/main.js')}}" ></script>
    </body>
</html>

Now, Last step We have to run or CLI below command for update simple main.js file:

Read Also:  VueJS String substring() Method

npm run dev

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Vue js Laravel 5.8 File Upload Tutorial.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.