Laravel 8 Single Image File Upload With Validation

In this tutorial you will learn about the Laravel 8 Single Image File Upload With Validation and its application with practical example.

Laravel 8 Single Image File Upload With Validation

In this Laravel 8 Single Image File Upload With Validation Tutorial Example, we will learn how to upload and save single image file in laravel application database. I’ll show you how to upload file in Laravel 8 application, store all the image into the MySQL database.

In this laravel simple 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 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 success message on the screen.

Table of Contents

  1. Create Laravel 8 Project
  2. Database Configuration
  3. Create Model and Migrations
  4. Create Controller
  5. Create Routes
  6. Create Blade File
  7. Start Laravel Server

1). Install Laravel Fresh App

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

2). Setup Database

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.

3). Generate Image Migration & Model

Now, in this step we will create model and migration file. Please run the following command:

After that, open create_photos_table.php file inside LaravelImage/database/migrations/ directory. And the update the function up() with following code:

Now, in this step we will create model and migration file. Please run the following command:

4). Create Image Upload Route

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

5). Create Image Upload Controller

Now, lets create a controller named UploadImageController using command given below –

Now, go to app/http/controllers and open UploadImageController.php file. And update the following code into it:

6). Create Blade View

Now we will create a blade view file. Go to app/resources/views and create one file name image.blade.php :

7). Create Images Directory inside Storage/app/public

Now, create images directory inside storage/app/public directory.

8). 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 Single Image File Upload With Validation and its application with practical example. I hope you will like this tutorial.