Single Image Upload Example using VueJS & Laravel
n this blog post let’s go through the steps involved in building a form in Laravel using VueJS that also has a file input field wherein the user can attach an image.
This post assumes that you already have a Laravel project set up on your local.
# Setup VueJS
If your project doesn’t have VueJS setup, Follow along the steps in this tutorial to get the install the Vue library in the Laravel project Laravel 7 Installation with Vue JS
# Prepare Model, Controller and Migration File
You can skip this step, if are just interested in the Vue code that handles the file upload.
For the demonstration, let’s work with an example we will create Posts.
Run the following artisan command to generate a model along with Controller and a Migration file.
Modify the migration file named create_posts_table
to include three new fields named title, description and picture
Make sure your project is connected to a database, and run the following command to create the tables into your database.
Add the following route entry into the web.php file
Let’s modify the code of create
method of the PostController to return the view file.
# Setting up Vue Component
Let’s dive into the details of the Vue Component. Create a new file named PostCreate.vue
inside resources / js / components.
Template
For the simplicity of the demonstration we will be using inline template. Thus I am keeping the template tags inside the component blank
Data properties
Our form is supposed to have three fields named title, description and a picture we have defined an object named formFields that contains the data property for these three fields.
# Form Submission
We have used the inline-template for our component. We have used the v-bind directive of VueJS to bind different property of the instance to the form.
Notice that we have used two event listeners attached to the form. One is on the change of the file input on which we call the onFileChange
method of the instance and another on the form submit event on which we call the submitForm
method of the instance.
onFileChange method
Once user selects the image in the file input it triggers the onFileChange method and this is where we get the file from the target input and assign it to the data property named picture
.
Notice that instead of directly sending the formFields object to the axios, we are using a Javascript object named FormData. We append all the fields inside formData and then send the formData object to the axios.
The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to “multipart/form-data”.
On the backend (i.e. Laravel ) let’s modify the store method of the PostController to handle the request sent from the frontend.
# Preview Image
How about showing a little thumbnail preview to the user on the webpage of the image he has selected for uploading. We can achieve this by using the FileReader javascript API.
Introduce two new data properties to the Vue Component
imagePreview will be used to store the actual image and showPreview will be used to determine weather to show the image preview.
Let’s modify the onFileChange method to invoke a new FileReader object and also attach a load event to the reader so that it knows when to show the image.
Add the following html just after the file input in your form.
We have used the v-bind directive to change the image source just after user selects the file.
That’s all about showing the image preview and uploading file to the form in VueJS.
0 Comments
CAN FEEDBACK
Emoji