Laravel 5.8 Multiple Image Upload with Preview

In this tutorial you will learn about the Laravel 5.8 Multiple Image Upload with Preview and its application with practical example.

Laravel 5.8 Multiple Image Upload with Preview

In this Laravel 5.8 Multiple Image Upload with Preview example, we will learn how to upload image along with preview before upload in laravel 5.8. In this laravel 5.8 multiple image upload example, I’ll show you how to validate upload image into folder and then save it into database. In this tutorial before saving image into database we will show preview of the images and then save it into directory. Before uploading the image we will display preview of the image. After successfully image upload into the database and folder we will display success message on the screen.

Install Laravel 5.8

First of all we need to create a fresh laravel project, download and install Laravel 5.8 using the below command

Configure Database In .env file

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

.env

Generate Migration

Now, we have to define table schema for photos table. Open terminal and let’s run the following command to generate a migration file to create photos table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. lets open migration file and put following code in it –

Run Migration

Now, run following command to migrate database schema.

After, the migration executed successfully the photos table will be created in database along with migrations, password_resets and users table.

Create Model

Next, we need to create a model called Photo using below command.

Once, the above command is executed it will create a model file Photo.php in app directory.

Create Controller

Now, lets create a controller for multiple image uploading. Create a controller named ImageUploadController using command given below –

Once the above command executed, it will create a controller file ImageUploadController.php in app/Http/Controllers/multiple_image_upload/ directory. Open the ImageUploadController.php file and put the following code in it.

app/Http/Controllers/multiple_image_upload/ImageUploadController.php

Here In the controller, we have following methods –

index() :- It displays Image Upload Form along with Uploaded Image

store() :- To Upload and Save Multiple Image in database .

Note:- Before uploading any file make sure you have created following two directory in the public folder called profile_images.

Create Blade / View Files

In this step, we will create view/blade file to display Image Upload Form. Lets create a blade file “index.blade.php” in “resources/views/multiple_image_upload/” directory and put the following code in it respectively.

resources/views/multiple_image_upload/index.blade.php

Create Routes

After this, we need to add following routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.

routes/web.php

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

http://localhost:8000/preview-multiple-image-upload

Output:-

laravel-5-8-multiple-image-upload-with-preview-1

After Image Upload Screen Output:-

Select Multiple Image files to upload.

laravel-5-8-multiple-image-upload-with-preview-2
Now, upload image.

laravel-5-8-multiple-image-upload-with-preview-3

In this tutorial we have learn about the Laravel 5.8 Multiple Image Upload with Preview and its application with practical example. I hope you will like this tutorial.