Laravel File Upload Tutorial Example Step By Step From Scratch

Welcome to Laravel File Upload Tutorial Example Step By Step From Scratch.

In this tutorial, I will guide you to upload file in your laravel project.

We will get the file from user’s computer and then we will store it in our laravel project.

Along with file, we will also insert the details like name and path of the file into the MySQL database.

First of all, just look at the below video demonstration.


Step 1. Make a new Project and Database

Fire up the below command first,

  • Above command will make a new project named “larafileupload”

Now let us create a new database. I am using sequel pro as a database administration tool.

  • If you are using other tools like phpMyAdmin or else than don’t worry, process is same.
  • First of all, create a new database called “larafileupload”
  • Now come to your laravel project and open your .env file.

Inside this .env file, you need to change below details.

  • My username is root and password is empty. You should fill above fields as per your configuration.

After this, we have successfully connected our project with MySQL database.

Step 2. Making a Table in Database

After database, let us make a new table in this “larafileupload” database.

So in your terminal, run the following command

  • This command will create a new migration file.
  • Navigate to app->database->migrations folder. Inside migrations folder, you will find a new file named “time_create_cars_table.php”

This file contains the below source code

  • I have changed the up() function in the above file.
  • All four lines in the up() function represents the name of the columns of the Cars table.

Now fire the below command

  • When this command triggers, system will create a new table named “Cars” in “larafileupload” database.

Step 3. Code for Model and Controller

Again we need to fire the below command

  • Below command will try to create controller file. Before making controller it will check whether a model called “Car” is there or not.

Because model class do not exist, it will check whether you want to create model or not? write “yes” here. See the below

  • So, as per above lines, a controller class called “CarsController.php” and a model class called “Car.php” is created.

Now in your Car.php write down the below source snippet.

I have just added one line in the above file.

  • Now go to app->Http->Controllers->CarsController.php

CarsController.php file should contain the following coding lines

This controller lines have several functions. I will explain all these functions later.

Step 4. Writing Routes

Now Navigate to app->routes->web.php file.

Source snippet for this file is as the below

I have added below two lines which are defining custom routes.

  • First line is defining one get route. It will access the index() function from CarsController.php file.
  • Second line is making post route which is referring storeCar() function.

Making Images Folder

Make a new folder inside public folder. Name of this new folder should be “images”

We will upload our files in this “images” folder.

Step 5. Blade File

Create a new blade file named “layout.blade.php” under resources->views file.

Add the following source lines in this “layout.blade.php” file.

Now create another blade file named “uploadfile.blade.php”

In this file, you need to add the below source snippet

  • This file create the main look and feel of the example.
  • There is one text field and two buttons are there.
  • When the user clicks on “Choose File” button, a pop up window will be opened and user can select image from this window.
  • Second button is “Create Car” . When it is clicked, system will upload the image into the public->images folder. Also, it will insert the name and path of the image into the database.
  • On the click event of “Create Car” button, laravel will make the post request to the URL /uploadsql
  • This URL will execute the storeCar() method from the CarsController.php file.

Below is the source lines for storeCar() method

  • $destinationPath will get the path to the images folder.
  • $image will get the original file using the $request variable.
  • $input[‘imagename’] will hold the value of current time and we will use this variable as the name of the uploaded file.

Below line is uploading the path to the images folder.

  • $dbPath will hold the value which system will add into the path column in database.
  • Finally, the $car variable will use the save() method to input values into the database.
  • Now run your project using “php artisan serve” command.

Go to “” and yes, your upload file example is done.

Download Code For Laravel File Upload

Click here to download source code from github

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *