Laravel 9 Image Upload Example Code Tutorial

Laravel 9 Image Upload Example Code Tutorial

If you are looking for how to make image upload functionality in laravel then you are in the right place.

Because in this tutorial, I will show you how to make upload image functionality with validation rules in the latest version laravel 9. We will first install the laravel 9 and then see the example code of image upload functionality.

I will show step by step example guide. So you can understand the code and integrate an image upload functionality in your laravel 9 projects.

Go check out our previous tutorial about Eloquent Mutators and Accessors in Laravel 9.

Let’s get started on image upload functionality in laravel.

Image Upload Example in Laravel 9

So in this laravel image upload tutorial, we will start from scratch so you can easily understand each piece of code.

We will install the laravel 9 latest version of it, database configuration, and create a migration, model, and controller.

And final we will create two routes, one will be a GET request to show the image upload form and another will be a POST request to save the image in the database.

So let’s start with installing laravel project.

Install Laravel Project

To install the laravel project, you need to run the following command in your terminal. Make sure you are at that place where you want to create a project directory.

For more details, see our step by step guide on how to install composer and laravel?

Database Configuration

After installing the laravel, we have to set up the database settings. To configure the database connection in laravel, open the .env file in the project root directory.

Configure the above details as per your’s database and server details.

Create Migration

To create the tables in a database in laravel, you need migration files that have table columns defined. So run the following command to create the migration file in the database/migrations folder.

Now open this migration file and make changes as per your requirements. For this tutorial, I’m taking the following columns.

After making changes in the up() function of the migration file, run the following command to migrate this and create a table in a database.

Create Model

To create the model in laravel, run the following command in your terminal. It will create a new model file in your project’s app/Models folder.

Open the app/Models/Product.php file and add all fields in the $fillable variable. It is important to make all fields writeable because we are going to save data in these fields.

Create Controller

Open your machine’s terminal and run the below command to create a controller in laravel. This is the file where we are going to save the image in the database.

You can also create a migration, model, and controller with resources only in one command, check that command here.

Go to the app/Http/Controllers folder and open the ProductController.php file. We will use index() and store() functions.

The index() function will use to show the image upload form (you can say product upload form with image) and the store() function will use to save the product details along with the image.

In the above store function, first, we check whether the image extensions allow or not along with a max of 5MB size.

Then we made a condition while checking that request has a file and get the image original name from the request using the getClientOriginalName() function.

In the next line, we store the image in the storage folder in the uploads directory, and in the next line, we save the image file path in the database by referencing the /storage folder.

And next, we store all other details for the product in an instance and then use the save() function to save them into the database.

Store Image in Public Folder
Store Image in S3
Store Image in Storage Folder

Create Blade Files

Now let’s create the blade view file for showing the product upload form with the image and all other details.

Create a product-form.blade.php file in the /resources/views folder. You can also create a new folder inside the view folder.

/resources/views/product-form.blade.php

In this blade view file, we made a form that has product name, description, price, SKU, and of course image upload fields, which is the main point of this tutorial.

In the form tag, make sure you have added the POST method, enctype attribute, and action route.

Let’s now create the routes for showing the form and saving the product fields data and also with images.

Create Routes

Add the routes on the web.php file in the /routes folder (routes/web.php), so simply take a reference of the below code and add the routes of your application.

Run Laravel Application

If you have successfully done all the above steps, so now it’s time to run our functionality which we have built to upload image with laravel 9 version. To run the laravel application, you need to run the following command in the terminal.

It will start to serve your laravel application in your browser or give you a link with a port to check your application.

Hope you understand all code and each step about how to upload image in laravel latest version 9. If you still face any issues or have any queries please do let me know in the comment section. I will help you with that. Thanks.

You May Also Like

About the Author: Aman Mehra

Hey! I'm Aman Mehra and I'm a full-stack developer and have 5+ years of experience. I love coding and help to people with this blog.

Leave a Reply

Your email address will not be published.