Laravel Excel
Laravel Excel first version was released in 2013 November 9. It's been a long since its first release. The latest version is Laravel Excel 3.1.20. It also has released two versions that support Laravel Nova. Laravel Excel uses phpSpreadsheet which is simple. PhpSpreadsheet is a library written in pure PHP and provides a set of classes which allows you to read and write different spreadsheet file formats like Excel, LibreOffice Calc. It has the following features. You can effertlessly export collections to Excel You can export queries with automatic chunking which improves the performance. Queue exports You can export Blade views to Excel Effortlessly imports to collections. Reads Excel file in chunks Import inserts are handled in batches Requirements to install and use Laravel Excel are the following. PHP: ^7.0 Laravel: ^5.5 PhpSpreadsheet: ^1.6 PHP extension php_zip enabled PHP extension php_xml enabled PHP extension php_gd2 enabled PHP extension php_iconv enabled PHP extension php_simplexml enabled PHP extension php_xmlreader enabled PHP extension php_zlib enabled How to install laravel Excel? If you have already installed all the above requirements in your system, open your composer.json file and require the package in your project. The composer will download PhpSpreadsheet and the package.
composer require maatwebsite/excelIf Maatwebsite\Excel\ExcelServiceProvider is not auto-discovered and registered by itself, register it yourself by adding ServiceProvider in config/app.php.
'providers' => [ /* * Package Service Providers... */ Maatwebsite\Excel\ExcelServiceProvider::class, ]
The Excel facade will also get auto-discovered as follows.
'aliases' => [ ... 'Excel' => Maatwebsite\Excel\Facades\Excel::class, ]
To publish all the configurations run the artisan command.
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
How to Export using Laravel Excel?
Create export class in app/Exports using make:export artisan command.
php artisan make:export UsersExport --model=UserThis file can be located in app/Exports.In app/Exports you can create export manually as shown below.
<?php
namespace App\Exports;
use App\User;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
public function collection()
{
return User::all();
}
}
Then call the exports function in your Controller as shown.
<?php
namespace App\Http\Controllers;
use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;
class UsersController extends Controller
{
public function export()
{
return Excel::download(new UsersExport, 'users.xlsx');
}
}
Lastly add the route to access the export. Your exported downloads folder.
Route::get('users/export/', 'UsersController@export');
How to perform Import using Laravel Excel?
In app/Imports create import class using make:import artisan command.
php artisan make:import UsersImport --model=User
Your imports file will be in app/Imports.
To manually create it paste the code given below in app/Imports.
<?php
namespace App\Imports;
use App\User;
use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Concerns\ToModel;
class UsersImport implements ToModel
{
/**
* @param array $row
*
* @return User|null
*/
public function model(array $row)
{
return new User([
'name' => $row[0],
'email' => $row[1],
'password' => Hash::make($row[2]),
]);
}
}
Call import function in your controller.
use App\Imports\UsersImport; use Maatwebsite\Excel\Facades\Excel; use App\Http\Controllers\Controller; class UsersController extends Controller { public function import() { Excel::import(new UsersImport, 'users.xlsx'); return redirect('/')->with('success', 'All good!'); } }
Now you can find imported users in your database.
Comments
0 comments
Please Sign in or Create an account to Post Comments