Laravel 6 Image Uploading using Ajax Tutorial with Example

In this tutorial you will learn about the Laravel 6 Image Uploading using Ajax Tutorial with Example and its application with practical example.

Laravel 6 Image Uploading using Ajax Tutorial with Example

In this Laravel 6 Image Upload using Ajax example, we will learn how to upload and save image in laravel 6 using ajax. In this laravel ajax image upload example, I’ll show you how to validate upload image into folder and then save it into database using ajax. In this tutorial before saving image into database we will validate image and then save it into directory using. Before uploading the image we will perform server side validation. After successfully image upload into the database and folder we will display uploaded image on the screen.

Install Laravel 6

First of all we need to create a fresh laravel project, download and install Laravel 6 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.

Recommended:-  Simple User Registration Form Example in AngularJS

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 simple 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/ajax_image_upload/ directory. Open the ImageUploadController.php file and put the following code in it.

app/Http/Controllers/ajax_image_upload/ImageUploadController.php

Here In the controller, we have following methods –

Recommended:-  Laravel 5.8 Ajax Form Submit With Validation

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

store() :- To Upload and Save 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 generate and display Image Upload Form. Lets create a blade file “index.blade.php” in “resources/views/ajax_image_upload/” directory and put the following code in it respectively.

resources/views/ajax_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.

Recommended:-  How To Fix No application encryption key has been specified error In Laravel

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/ajax-image-upload

Output:-

laravel-image-uploading-using-ajax-1

After Image Upload Screen Output:-

laravel-image-uploading-using-ajax-2

In this tutorial we have learn about the Laravel 6 Image Uploading using Ajax Tutorial with Example and its application with practical example. I hope you will like this tutorial.