An Excel (XLS) file is a spreadsheet with powerful data visualization. And Laravel is a PHP web framework that follows a model-view-controller design pattern.
If you are using the Laravel framework for your web development, and if you are a beginner in Laravel. Then this post will help you to learn something new in Laravel. Because in this post you can find how to import Excel spreadsheet data and insert it into the MySQL database in Laravel. Here for importing Excel file data here we will use the Laravel Maatwebsite package. By using this package we can easily import data from an Excel sheet and insert it into a table in Laravel application.
In some of the business houses in which there are many large numbers of data has been stored in Excel, and after storing that data, they want some application which helps them to store in the web application database, that means to import into the web application database. Then at that time if your web application has been made in PHP Laravel framework this post will help you to make importing excel file data feature in Laravel application by using Maatwebsite package.
In this post we will see or learn how can we import any type of Excel spreadsheet in .xls, .xlsx or .csv data imported into Mysql database in Laravel. Here, we have to use Maatwebsite Laravel Excel package, this package will help to communicate excel file data in the Laravel application. Below you can find complete step by step process of how to use Laravel maatwebsite package for import Excel sheet data into database in Laravel.
After this you have to make database connection. For this first, you have to open the database.php file from config and in this file you have to define your database configuration.
After this you have to open the .env file, and in this file also you have to define the Mysql database configuration.
If you want to import excel file data in Laravel, you have to first download Maatwebsite package, this package will communicate with Excel spreadsheet data. First, to download the package, you have to go to the command prompt and write the following command.
Composer Require Maatwebsite/Excel
This command will download this package in your Laravel working folder. After this, we have to register this package in our Laravel application. For this, you have to go to the config/app.php file. And in this file, you have to define providers and aliases.
Now we have to make controller for handle HTTP request for import data. In this controller, we have used two use statements. First use DB is used to do MySQL database operation, and second use Excel is for the Maatwebsite package for import excel sheet data. In this controller, we have made two methods.
index() - This is root method of this class, in this method it will fetch data from customer table and that data will be load in import_blade.php file in table format.
import() - This method has request for import excel file data. In this method first, it has validated excel file format. If a selected file other than an excel sheet then it will return a validation error. But suppose the selected file is excel then it will proceed for import data. For import data here it has called Excel package class which has get data from excel file and convert into PHP array and then after insert into customer table. After successfully import of data it will return a success message.
This file has been load by index() method of ImportExcelController, on this file we have to make a form for select excel file from the local computer for import data. Below the form, it will display tbl_customer table data, and in the above form, we have to define validation error message and success message.
After this we have to set the route of controller method. For this, we have to open to routes/web.php file. In this file, we can define the route.
Lastly, we have to run Laravel application, for this we have to go to command prompt, and write following command.
Php Artisan Serve
This command will Laravel application, it will return base url of Laravel application. Now for this application, we have to write the following URL in the browser.
http://127.0.0.1:8000/import_excel
Excel File
In this blog, we have shared a step-by-step guide with you on how a user can import his excel file to the database in the Laravel framework. This will help in adding multiple records at the same time.
With this guide, we have provided an ready to use codes, to help you in programming and have an easy understanding.
Project Manager at Vasundhara Infotech, a leading software development company. His great planning and execution qualities led to several successful projects.
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!
Sign Up to our newsletter to get latest updates staight in your inbox.
Vasundhara respects your privancy. No Spam!