Laravel 6.16 Import & Export Excel to Database Example
In Laravel 6.16, you can easily import and export Excel files using the Maatwebsite Excel package. Below is a step-by-step guide on how to implement it.
1. Install Maatwebsite Excel Package
Run the following command to install the package:
After installation, add the service provider and alias in config/app.php
:
Then, publish the config file:
2. Configure Database and Model
Ensure your .env
file is configured with database credentials:
Create a model and migration for the products
table:
Modify database/migrations/xxxx_xx_xx_create_products_table.php
:
Run migration:
3. Create Import & Export Classes
Create an Import Class
Modify app/Imports/ProductsImport.php
:
Create an Export Class
Modify app/Exports/ProductsExport.php
:
4. Create a Controller
Modify app/Http/Controllers/ProductController.php
:
5. Create Blade View for Import
Create resources/views/import.blade.php
:
6. Define Routes
Modify routes/web.php
:
7. Run the Application
Start the Laravel server:
Go to:
http://127.0.0.1:8000/import
Upload an Excel file with name
, price
, and description
columns.
Click Download Excel to export data.
Conclusion
✅ Import and export Excel in Laravel 6.16 using Maatwebsite Excel.
✅ Eloquent ORM makes data handling easy.
✅ Validation & Security ensures smooth operation.