Import Export Excel & CSV File in Laravel
In this tutorial, we will learn how to import export excel & csv file in laravel. i written simple tutorial of laravel maatwebsite/excel. using maatwebsite/excel we can import export excel or csv from database in laravel application.
Install Laravel maatwebsite/excel package
Now install the Laravel maatwebsite/excel version 3.1 package via composer command.
composer require maatwebsite/excel
This package has auto-discovery features so you don't need to add the service provider manually.
Make an Import class for import data.
Now we have to make an Import class for importing data into our database from Excel sheet via our contact model. The maatwebsite/excel package provides useful command to make the class easily. Let's do that.
php artisan make:import ContactsImport --model=Contact
with this command, a Contact import class will create in the app/Imports
directory.
<?php
namespace App\Imports;
use App\Contact;
use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Concerns\ToModel;
class ContactsImport implements ToModel
{
/**
* @param array $row
*
* @return Contact|null
*/
public function model(array $row)
{
return new Contact([
'name' => $row[0],
'phone' => $row[1],
'email' => $row[2]
]);
}
}
Make an Export class for export data
For export data from our database to Excel sheet, we need to make an Export class via command line.
php artisan make:export ContactsExport --model=Contact
By this command, a class file will create in app/Exports
directory.
<?php
namespace App\Exports;
use App\Contact;
use Maatwebsite\Excel\Concerns\FromCollection;
class ContactsExport implements FromCollection
{
public function collection()
{
return Contact::all();
}
}
Make a controller for handle excel data import or export
Now the final part where we'll handle how we'll export or import data and what will be the logic goes to. Let's make a controller by artisan make command.
php artisan make:controller ContactController
Handle Excel data import
To import data from excel sheet, make an import method inside the contact controller. In this import method, we'll validate our request file and code for import the excel sheet data.
<?php
use App\Imports\ContactsImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class ContactsController extends Controller
{
public function import(Request $request)
{
$this->validate($request, [
'file' => 'required|file|mimes:xls,xlsx'
]);
$file = $request->file('file');
Excel::import(new ContactsImport, $file);
return redirect()->back()->with('success', 'All data successfully imported!');
}
}
Handle Excel data export
With the data export feature, we can export our table data into an excel sheet very easily. For that, make an export method inside the contact controller and do code for model data export into an excel sheet.
<?php
namespace App\Http\Controllers;
use App\Exports\ContactsExport;
use Maatwebsite\Excel\Facades\Excel;
class ContactController extends Controller
{
public function export()
{
return Excel::download(new ContactsExport, 'contacts.xlsx');
}
}
I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.