Laravel 6 Import Export Excel CSV File to Database

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

Laravel 6 Import Export Excel CSV File to Database

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

When we are developing laravel applications, there may situations where we require to import or export large amount of data into and from the application database. Importing and exporting 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. The maatwebsite/excel is a composer package used to import and export excel or csv files. The maatwebsite/excel provide number of features for excel or csv file import and exort in laravel.

Create Laravel 5.8 Application

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

.env

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.

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

app/Contact.php

Install Maatwebsite Package

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

Recommended:-  Laravel 6 Image Upload With Validation

Configure Maatwebsite Package

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

config/app.php

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 ImportExportExcelController. Use the following command to create import class.

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

Imports/ImportContacts.php

Create Export Class

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

After, the above command executed successfully it will create ExportContacts.php file in Exports directory.

Exports/ExportContacts.php

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 ImportExportExcelController using command given below –

Once the above command executed, it will create a controller file ImportExportExcelController.php in app/Http/Controllers/ImportExportExcel directory.

Open the ImportExportExcel/ImportExportExcelController.php file and put the following code in it.

app/Http/Controllers/ImportExportExcel/ImportExportExcelController.php

Here In the controller, we have following methods –

index() :- It displays File Upload Form and Contact data.

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

export() :- To export and download excel file for 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_export_excel/” directory and put the following code in it respectively.

resources/views/import_export_excel/index.blade.php

Create Import Export 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 5.7 Google ReCAPTCHA Integration

routes/web.php

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 –

http://127.0.0.1:8000/import-export-excel

Output:-

laravel-6-import-export-excel-file-in-database-1

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

laravel-6-import-excel-csv-file-in-database-1

laravel-6-import-export-excel-file-in-database-2

Now, import file.

After Upload Screen Output:-

laravel-6-import-export-excel-file-in-database-3

Now, click “Export File” to export data into csv file.

laravel-6-import-export-excel-file-in-database-4

I hope you like this laravel 6 Import Export Excel CSV File to Database tutorial.

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