Laravel 6 Import Excel CSV File to Database Using Maatwebsite

In this tutorial you will learn about the Laravel 6 Import Excel CSV File to Database Using Maatwebsite and its application with practical example.

Laravel 6 Import Excel CSV File to Database

In this tutorial, I’ll show you how to import Excel spreadsheet or csv file into database in laravel 6 using maatwebsite version 3 package with example. I’ll guide you through step by step with example of importing a csv or excel file using maatwebsite/excel version 3 composer package.

When we are working with some large applications, there may situations where we require to import large amount of data into the application database. Importing data through excel or csv files seems a good solution here. In laravel, maatwebsite/excel composer package made it easy to import or export excel or csv files.

Maatwebsite version 3 package:-

The maatwebsite/excel is a composer package used to import and export excel or csv files. This package made it easy to import and export data using database model. We will be using maatwebsite/excel version 3, there are many new changes in version 3. It will work with laravel version like 5.8, 5.7 .

In this laravel import excel example, we will display a form to upload csv file and to display uploaded customer data. When the excel file is imported into database we will display imported data into the screen along with the success message.

Before starting with example I assume that you already have fresh laravel 6 installation ready, if you have it installed then you can skip this step.

Create Laravel 6 Application

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


Create Model and Migration

Now, we have to define table schema for contact table. Open terminal and let’s run the following command to generate a Contact model along with a migration file to create contact table in our database.

Once this command is executed you will find a migration file created under “database/migrations”. Lets open migration file created and put following code in it –

Run Laravel Migration

Now, run following command to migrate database schema.

Recommended:-  How to Get Site URL In Laravel

After, the migration executed successfully the contact table will be created in database along with a model file Contact.php in app directory.


Install Maatwebsite Package

In this step, we will install Maatwebsite Package via the composer dependency manager. Use the following command to install Maatwebsite Package.

Configure Maatwebsite Package

After Installing Maatwebsite package, we need to add service provider and alias in config/app.php file as following.


Now, use following command to publish Maatwesite Package configuration file:

This will create Maatwesite Package configuration file named “config/excel.php”.

Create Import Class

Now we will create a import class for Contact model to use in our ImportExcelController. Use the following command to create import class.

After, the above command executed successfully it will create ImportContacts.php file in Imports directory.


Create Import Excel Controller

Next, we have to create a controller to display a form to upload excel file records. Lets Create a controller named ImportExcelController using command given below –

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

Recommended:-  Single Page Application with AngularJS Routing and Templating


Here In the controller, we have following methods –

index() :- It displays File Upload Form.

import() :- To Upload excel file and Save records in database .

Create Blade / View Files

In this step, we will create view/blade file to upload excel file. Lets create a blade file “index.blade.php” in “resources/views/import_excel/” directory and put the following code in it respectively.


Create Import Excel 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.

Recommended:-  Laravel Clear View Cache


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 –




Select excel file to upload, before uploading you must have created an excel file as following:



Now, import file.

After Upload Screen Output:-



In this tutorial we have learn about the Laravel 6 Import Excel CSV File to Database Using Maatwebsite and its application with practical example. I hope you will like this tutorial.