Laravel 8 Image Upload Example Tutorial

In this tutorial you will learn about the Laravel 8 Image Upload Example Tutorial and its application with practical example.

In this Laravel 8 Image Upload Example Tutorial, we will learn how to upload and save image in laravel 8. In this Laravel 8 Image Upload Example Tutorial, 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 validate image and then save it into directory using. Before uploading the image we will perform server side validation. You will also learn how to validate image mime type, size, dimension, etc on laravel controller by using laravel validation rules. After successfully image upload into the database and folder we will display success message on the screen.

Image Upload In Laravel 8

In this step by step tutorial I’ll guide you through the process to upload image on public storage and directory in laravel 8:

  • Download Laravel 8 Application
  • Setup Database with App
  • Create Model & Migration
  • Create Routes
  • Create Controller By Artisan Command
  • Create Blade View
  • Create Images Directory inside Storage/app/public
  • Run Development Server

Install Laravel 8

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

Create Model & Migration

Now, we have to define table schema for photos table. Open terminal and let’s run the following command to generate a migration along with model 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 –

Now, run following command to migrate database schema.

After, the migration executed successfully the photos table will be created in database.

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

Create Controller By Artisan Command

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

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

app/Http/Controllers/UploadImageController.php

Note:- Before uploading any file make sure you have created following directory in the storage/app/public folder called images.

Create Blade View

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

resources/views/image.blade.php

Create Images Directory inside Storage/app/public

Before uploading any file make sure you have created following directory in the storage/app/public folder called images.

Run Development Server

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 –

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