How to Upload and Store Files with Laravel 8

If you need to create a form that requires users to provide a picture or some other media, this article will prove useful to you. We will be taking a look at how you create a form with a drop zone that allows users to drag-and-drop files they wish to upload as well as what you will need on your Laravel backend to store these files. This tutorial assumes you know your way around the file system of a Laravel project. Let’s do this!!!

Case Scenario

Let’s assume we need to store the name, gender and picture for each student in the school. We therefore create a form where students can provide these details. In our MySQL database, we have a table called students that has columns called name, gender and file_path to hold the name, gender and the path to the picture of each student respectively (The table also has default columns provided by Laravel such as created_at and updated_at).

1. Create a new laravel project

We will name our laravel project students. To create it we will open our command line, navigate to the folder we wish to store our project in and run the following line:

Run the following line to navigate into the folder:

Don’t forget to update your .env file with the appropriate database name.

2. Create a “Student” model and its associated controller and migration file

You can do this by running the following line of code on your command line:

3. Create a view where you can show your form

Navigate to your views folder and create a file named students.blade.php.

4. Add jQuery and Dropzone CDNs to your view file

The Dropzone CDNs will allow users to add files to the form via drag-and-drop. jQuery is included to help with form submission. Files dragged into dropzone widgets are submitted seperately from the rest of the form’s elements. Here are the CDNs you will need:

After adding the CDNs to your HTML layout in students.blade.php, your view file should look like this:

4. Edit your migration file

The file_path column is made nullable because its value is not provided along with the values for the other columns.

5. Run your migrations on the command line

Run the following command on your command line to create your tables in your database:

6. Create your form

Your form should something like this:

jQuery is used to submit the form data without the image after which it receives a response containing the student_id of the student whose information was just stored. It then submits the image from the drop zone along with the id received.

Make sure the meta csrf tag is added to your view or you might face problem.

7. Update your routes

In your routes/web.php file, add the following lines

As explained in step 4, we will need to upload our picture seperately from the rest of the form data. Two different routes are therefore provided accordingly.

To access the form on your browser:

  1. Start the server from your command line with the following command:

2. Type the following into the address bar of your browser and press enter

8. Update your controller

You will need to add three methods to your controller: the first will show the view where student data is entered, the second will store form data (without the image) and the third will store the student image. Your StudentController.php file should look like this.

9. Test it!! Fill the form and submit it

Now, if you go to storage/app folder, you will find a new folder named files and the uploaded image will be available in this folder.

If you want to give a name to the file you are uploading, you can use storeAs() method instead of the store() method used in the controller.

The file path that is saved to your database will look something like this

Happy Coding !!! All the best !!!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store