This is a package to manage async export and import in Backpack for Laravel
You can install the package via composer:
composer require thomascombe/backpack-async-export
You can publish and run the migrations with:
php artisan vendor:publish --provider="Thomascombe\BackpackAsyncExport\BackpackAsyncExportServiceProvider" --tag="backpack-async-export-migrations"
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --provider="Thomascombe\BackpackAsyncExport\BackpackAsyncExportServiceProvider" --tag="backpack-async-export-config"
This is the contents of the published config file:
return [
'feature_enabled' => [
'export' => true,
'import' => true,
],
'user_model' => 'App\Models\User',
'import_export_model' => Thomascombe\BackpackAsyncExport\Models\ImportExport::class,
'admin_export_route' => 'export',
'admin_import_route' => 'import',
'export_memory_limit' => '2048M',
'disk' => 'local',
];
php artisan backpack:add-sidebar-content "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('export') }}'><i class='nav-icon la la-file-export'></i> <span>Export</span></a></li>"
php artisan make:export UserExport --model=App/Models/User
For all details, have a look at Laravel Excel Package.
You can make your export class extends our LaravelExcel abstract.
php artisan backpack:crud {Name}CrudController
use Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Interfaces\ExportableCrud;
class {Name}CrudController extends CrudController implements ExportableCrud {}
use Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Traits\HasExportButton;
public function setup()
{
// ...
$this->addExportButtons();
}
use Thomascombe\BackpackAsyncExport\Enums\ActionType;
use Thomascombe\BackpackAsyncExport\Enums\ExportStatus;
use Thomascombe\BackpackAsyncExport\Models\ImportExport;
public function getExport(): ImportExport
{
return ImportExport::create([
ImportExport::COLUMN_USER_ID => backpack_user()->id,
ImportExport::COLUMN_ACTION_TYPE => ActionType::Export,
ImportExport::COLUMN_STATUS => ExportStatus::Created,
ImportExport::COLUMN_FILENAME => sprintf('export/users_%s.xlsx', now()->toIso8601String()),
ImportExport::COLUMN_EXPORT_TYPE => UserExport::class,
]);
}
public function getExportParameters(): array
{
return [];
}
It may sometimes be necessary to export large amounts of data. PhpSpreadsheet (used behind the hood by this package) does not always offer the best performance. In this case, it is recommended to use the low-level functions of PHP, such as fputcsv.
This package has an abstract class SimpleCsv which can be extended to use this export mode. Of course, it is more limited and only allows you to define a query, headers and a mapping between the model and the data table to be exported.
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Thomascombe\BackpackAsyncExport\Exports\SimpleCsv;
class UserExport extends SimpleCsv
{
public function query(): EloquentBuilder
{
return User::query();
}
public function headings(): array
{
return [
'ID',
'Name',
'Email',
];
}
/**
* @param User $user
* @return array
*/
public function map($user): array
{
return [
$user->id,
$user->name,
$user->email,
];
}
}
php artisan backpack:add-sidebar-content "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('import') }}'><i class='nav-icon la la-file-import'></i> <span>Import</span></a></li>"
php artisan make:import UserImport --model=App/Models/User
For all details, have a look at Laravel Excel Package
php artisan backpack:crud {Name}CrudController
use Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Interfaces\ImportableCrud;
class {Name}CrudController extends CrudController implements ImportableCrud {}
use Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Traits\HasImportButton;
public function setup()
{
// ...
$this->addImportButtons();
}
use Thomascombe\BackpackAsyncExport\Enums\ActionType;
use Thomascombe\BackpackAsyncExport\Enums\ExportStatus;
use Thomascombe\BackpackAsyncExport\Models\ImportExport;
public function getImport(): ImportExport
{
return ImportExport::create([
ImportExport::COLUMN_USER_ID => backpack_user()->id,
ImportExport::COLUMN_ACTION_TYPE => ActionType::Import,
ImportExport::COLUMN_STATUS => ExportStatus::Created,
ImportExport::COLUMN_FILENAME => '',
ImportExport::COLUMN_EXPORT_TYPE => UserImport::class,
]);
}
public function getImportParameters(): array
{
return [
'private' => [
'hint' => 'CSV file required',
'mimetypes' => ['text/csv', 'application/csv'],
],
];
}
You can override ImportExport
model using config : import_export_model
.
Your model class need to implement \Thomascombe\BackpackAsyncExport\Models\ImportExport
.
class ImportExport extends \Thomascombe\BackpackAsyncExport\Models\ImportExport
{
}
Package allow to change export name with interface on your export class: Thomascombe\BackpackAsyncExport\Exports\ExportWithName
namespace App\Exports;
use Thomascombe\BackpackAsyncExport\Exports\ExportWithName;
class UserExport implements ExportWithName
{
public static function getName(): string
{
return 'My export name';
}
}
You can easily have multi export on save CRUD.
Your CRUD controller need to implement Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Interfaces\MultiExportableCrud
interface.
public function getAvailableExports(): array
{
return [
'default' => null,
'all' => 'All',
];
}
Array keys: key for query params and dynamic method name
Array values: Export name (display in CRUD button)
For each new export you have to add news methods:
public function getExport*All*(): ImportExport
{
return ImportExport::create(...);
}
public function getExport*All*Parameters(): array
{
return [...];
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.