- First install maatwebsite/excel package:
composer require maatwebsite/excel
-
Copy
export.blade.php
from this gist inresources/views/vendor/backpack/crud/buttons/export.blade.php
-
Copy
ExportOperation.php
from this gist inapp/Traits/Operations/ExportOperation.php
-
Add the operation to your Crud controller
<?php
namespace App\Http\Controllers\Admin;
...
class UsersCrudController extends CrudController
{
...
use \App\Traits\Operations\ExportOperation; // add this line to activate the operation
}
- DONE 😁 the export button should appear
<?php
namespace App\Http\Controllers\Admin;
...
use PhpOffice\PhpSpreadsheet\Shared\Date as ExcelDate; // add this only if you want to use an excel date format
class UsersCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
use \App\Traits\Operations\ExportOperation; // add this line to activate the operation
protected function setupListOperation()
{
CRUD::column('image');
CRUD::column('name');
CRUD::column('email')->label('Email Address');
// by default the export uses the same columns that are defined in the list operation
// the header row in the final export uses the label attribute
}
public function getExportExtraColumns()
{
// with this function you can remove or add new columns in the final export file
CRUD::column('image')->remove();
CRUD::column('id')->makeFirst();
CRUD::column('created_at');
}
public function getExportMapping($entries, $type): array {
// with this function you can customize the data processing and format
return $entries->map(function ($item) use ($type) {
$data = $item->only(['id', 'name', 'email']);
// this is how to make a column type date only in the excel export
$data['created_at'] = $type == 'excel' ? ExcelDate::dateTimeToExcel($item->created_at->format('Y-m-d'))
: $item->created_at->format('Y-m-d');
return $data;
})->toArray();
}
public function getExportFormat(): array {
// for a date column in excel you also need this function
// find other formats here: https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Style/NumberFormat.php
return [
'B' => \PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDD,
];
}
// you can also extend this with another format if you need
public function getExportTypes(): array
{
return [
// defaults
'csv' => 'CSV',
'excel' => 'Excel',
// custom
'pdf' => 'PDF',
];
}
public function exportAsPdf($data)
{
// $data['entries']
// $data['columns']
// $data['filename']
// you must return the file for view, or with some headers for download.
}
// with this function you can customize the filename
public function getExportFilename(): string
{
// by default it does something like: Export-Users-2022-05-25
return 'All users '.date('Y-m-d H:m');
}
}