Laravel Excel
1. Installation (if not already installed) If the package is not already installed, run the following in the terminal:
composer require maatwebsite/excel
2. Creating an Export Class Laravel Excel uses special classes to generate files.
Create it with the following command:
php artisan make:export DealsExport --model=Deal
Edit the created file app/Exports/DealsExport.php so that it exports only the necessary fields, not all:
namespace App\Exports;
use App\Models\Deal;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class DealsExport implements FromCollection, WithHeadings
{
public function collection()
{
// Выгружаем имя сделки и название стадии
return Deal::with('stage')->get()->map(function($deal) {
return [
'ID' => $deal->id,
'Name' => $deal->name,
'Stage' => $deal->stage->name,
'Created At' => $deal->created_at->format('d.m.Y'),
];
});
}
public function headings(): array
{
return ['ID', 'Deal Name', 'Stage', 'Date Created'];
}
}
Exporting collections
Create a new class called InvoicesExport in app/Exports:
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromCollection;
class InvoicesExport implements FromCollection
{
public function collection()
{
return Invoice::all();
}
}
In your controller we can now download this export:
public function export()
{
return Excel::download(new InvoicesExport, 'invoices.xlsx');
}
Optionally you can pass in whether or not to output headers and custom response headers:
public function export()
{
return Excel::download(new InvoicesExport, 'invoices.xlsx', true, ['X-Vapor-Base64-Encode' => 'True']);
} Or store it on a disk, (e.g. s3):
public function storeExcel()
{
return Excel::store(new InvoicesExport, 'invoices.xlsx', 's3');
}
Using custom structures
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromCollection;
class InvoicesExport implements FromCollection
{
public function collection()
{
return new Collection([
[1, 2, 3],
[4, 5, 6]
]);
}
}
Using arrays
If you prefer to use plain arrays over Collections, you can use the FromArray concern:
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromArray;
class InvoicesExport implements FromArray
{
public function array(): array
{
return [
[1, 2, 3],
[4, 5, 6]
];
}
} If you need to pass data from the controller to your export, you can use the constructor to do so:
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromArray;
class InvoicesExport implements FromArray
{
protected $invoices;
public function __construct(array $invoices)
{
$this->invoices = $invoices;
}
public function array(): array
{
return $this->invoices;
}}
In your controller you can now use the constructor of the export class:
public function export()
{
$export = new InvoicesExport([
[1, 2, 3],
[4, 5, 6]
]);
return Excel::download($export, 'invoices.xlsx');
}
Dependency injection
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
class InvoicesExport implements FromCollection
{
public function __construct(InvoicesRepository $invoices)
{
$this->invoices = $invoices;
}
public function collection()
{
return $this->invoices->all();
}
}
public function export(Excel $excel, InvoicesExport $export)
{
return $excel->download($export, 'invoices.xlsx');
}
Strict null comparisons
If you want your 0 values to be actual 0 values in your Excel sheet instead of null (empty cells), you can use WithStrictNullComparison.
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
class InvoicesExport implements FromCollection, WithStrictNullComparison
{
public function __construct(InvoicesRepository $invoices)
{
$this->invoices = $invoices;
}
public function collection()
{
return $this->invoices->all();
}
}
Custom start cell
The default start cell is A1. Implementing the WithCustomStartCell concern in your export class allows you to specify a custom start cell.
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
class InvoicesExport implements FromCollection, WithCustomStartCell
{
public function collection()
{
return Invoice::all();
}
public function startCell(): string
{
return 'B2';
}
}
Storing raw contents
$contents = Excel::raw(new InvoicesExport, \Maatwebsite\Excel\Excel::XLSX);
The package provides some macro to Laravel’s collection class to easily download or store a collection.
Downloading a collection as Excel
User::all()->downloadExcel(
$filePath,
$writerType = null,
$headings = false
)
It doesn't have to be an Eloquent collection to work:
(new Collection([[1, 2, 3], [1, 2, 3]]))->downloadExcel(
$filePath,
$writerType = null,
$headings = false
)
Storing a collection on disk
User::all()->storeExcel(
$filePath,
$disk = null,
$writerType = null,
$headings = false
)