Skip to content

Repository files navigation

Social Card of Laravel Backpack Async Export

Laravel Backpack Async Export

Latest Version on PackagistPHPCS checkTotal Downloads

This is a package to manage async export and import in Backpack for Laravel

Demo of Laravel Backpack Async Export

Demo of Laravel Backpack Async Export

Installation

You can install the package via composer:

composer require webqamdev/backpack-async-export
VersionPHPLaravelBackpack
1.x7.4 - ^8.0^8.0 - ^9.0 - ^10.04.1.* - ~5.0
2.x^8.1^9.0 - ^10.0~5.5 - ~6.0
3.x^8.1^10.0~6.0
4.x^8.2^11.0~6.0
5.x^8.2^12.0 - ^13.0^7.0

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',
];

Usage for export

Add export item in menu

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>"# or 
php artisan backpack:add-menu-content "<x-backpack::menu-item title='Export' icon='la la-file-export' :link=\"backpack_url('export')\" />"

Create you export class

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.

Create your controller

php artisan backpack:crud {ModelName}

Your controller need to implement interface

useThomascombe\BackpackAsyncExport\Http\Controllers\Admin\Interfaces\ExportableCrud;
class {Name}CrudController extends CrudController implements ExportableCrud {}

Use awesome trait

use \Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Traits\HasExportButton;

Call method to add buttons

publicfunctionsetup()
{
// ...$this->addExportButtons();
}

Add method to your CRUD controller

useThomascombe\BackpackAsyncExport\Enums\ActionType;
useThomascombe\BackpackAsyncExport\Enums\ImportExportStatus;
useThomascombe\BackpackAsyncExport\Models\ImportExport;
publicfunctiongetExport(): ImportExport
{
return ImportExport::create([
ImportExport::COLUMN_USER_ID => backpack_user()->id,
ImportExport::COLUMN_ACTION_TYPE => ActionType::Export,
ImportExport::COLUMN_STATUS => ImportExportStatus::Created,
ImportExport::COLUMN_FILENAME => sprintf('export/users_%s.xlsx', now()->toIso8601String()),
ImportExport::COLUMN_EXPORT_TYPE => UserExport::class,
]);
}
publicfunctiongetExportParameters(): array
{
return [];
}

Simple csv export

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.

useIlluminate\Database\Eloquent\BuilderasEloquentBuilder;
useThomascombe\BackpackAsyncExport\Exports\SimpleCsv;
class UserExport extends SimpleCsv
{
publicfunctionquery(): EloquentBuilder
{
return User::query();
}
publicfunctionheadings(): array
{
return [
'ID',
'Name',
'Email',
];
}
/** * @param User $user * @return array */publicfunctionmap($user): array
{
return [
$user->id,
$user->name,
$user->email,
];
}
}

Usage for import

Add import item in menu

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>"

Create you import class

php artisan make:import UserImport --model=App/Models/User

For all details, have a look at Laravel Excel Package

Create your controller

php artisan backpack:crud {Name}CrudController

Your controller need to implement interface

useThomascombe\BackpackAsyncExport\Http\Controllers\Admin\Interfaces\ImportableCrud;
class {Name}CrudController extends CrudController implements ImportableCrud {}

Use awesome trait

useThomascombe\BackpackAsyncExport\Http\Controllers\Admin\Traits\HasImportButton;

Call method to add buttons

publicfunctionsetup()
{
// ...$this->addImportButtons();
}

Add method to your CRUD controller

useThomascombe\BackpackAsyncExport\Enums\ActionType;
useThomascombe\BackpackAsyncExport\Enums\ImportExportStatus;
useThomascombe\BackpackAsyncExport\Models\ImportExport;
publicfunctiongetImport(): ImportExport
{
return ImportExport::create([
ImportExport::COLUMN_USER_ID => backpack_user()->id,
ImportExport::COLUMN_ACTION_TYPE => ActionType::Import->value,
ImportExport::COLUMN_STATUS => ImportExportStatus::Created,
ImportExport::COLUMN_FILENAME => '',
ImportExport::COLUMN_EXPORT_TYPE => UserImport::class,
]);
}
publicfunctiongetImportParameters(): array
{
return [
'private' => [
'hint' => 'CSV file required',
'mimetypes' => ['text/csv', 'application/csv'],
],
];
}

Need more?

Override ImportExport model

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
{
}

Update export name

Package allow to change export name with interface on your export class: Thomascombe\BackpackAsyncExport\Exports\ExportWithName

namespaceApp\Exports;
useThomascombe\BackpackAsyncExport\Exports\ExportWithName;
class UserExport implements ExportWithName
{
publicstaticfunctiongetName(): string
{
return'My export name';
}
}

Demo with custom name of Laravel Backpack Async Export

Multi export by CRUD?

You can easily have multi export on save CRUD.
Your CRUD controller need to implement Thomascombe\BackpackAsyncExport\Http\Controllers\Admin\Interfaces\MultiExportableCrud interface.

publicfunctiongetAvailableExports(): 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:

publicfunction getExport*All*(): ImportExport
{
return ImportExport::create(...);
}
publicfunction getExport*All*Parameters(): array
{
return [...];
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

This is a package to manage async export in Backpack for Laravel

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages