namespace App\MoonShine\Resources;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Resources\ModelResource;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
}
namespace App\MoonShine\Resources;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
protected function importFields(): iterable
{
return [
ID::make(),
Text::make('Name'),
];
}
}
Be sure to add ID to the import; otherwise, records will only be added without updating existing ones.
If you need to modify a value during import, you should use the field method fromRaw().
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:8]
namespace App\MoonShine\Resources;
use App\Enums\StatusEnum;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\UI\Fields\Enum;
use MoonShine\UI\Fields\ID;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
protected function importFields(): iterable
{
return [
ID::make(),
Enum::make('Status')
->attach(StatusEnum::class)
->fromRaw(static fn(string $raw, Enum $ctx) => StatusEnum::tryFrom($raw)),
];
}
}
namespace App\MoonShine\Resources;
use App\Enums\StatusEnum;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\UI\Fields\Enum;
use MoonShine\UI\Fields\ID;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
protected function importFields(): iterable
{
return [
ID::make(),
Enum::make('Status')
->attach(StatusEnum::class)
->fromRaw(static fn(string $raw, Enum $ctx) => StatusEnum::tryFrom($raw)),
];
}
}
For configuring the import, optional methods are available.
To use them, you need to add the import() method that returns ImportHandler.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:8]
namespace App\MoonShine\Resources;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\ImportHandler;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Handlers\Handler;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\UI\Components\ActionButton;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
protected function import(): ?Handler
{
return ImportHandler::make(__('moonshine::ui.import'))
// Specify the IDs of users who will be notified when the operation is completed
->notifyUsers(fn(ImportHandler $ctx) => [auth()->id()])
// Select disk
->disk('public')
// Select directory for saving the import file
->dir('/imports')
// Delete the file after import
->deleteAfter()
// CSV delimiter
->delimiter(',')
// Modify button
->modifyButton(fn(ActionButton $btn) => $btn->class('my-class'))
;
}
}
namespace App\MoonShine\Resources;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\ImportHandler;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Handlers\Handler;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\UI\Components\ActionButton;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
protected function import(): ?Handler
{
return ImportHandler::make(__('moonshine::ui.import'))
// Specify the IDs of users who will be notified when the operation is completed
->notifyUsers(fn(ImportHandler $ctx) => [auth()->id()])
// Select disk
->disk('public')
// Select directory for saving the import file
->dir('/imports')
// Delete the file after import
->deleteAfter()
// CSV delimiter
->delimiter(',')
// Modify button
->modifyButton(fn(ActionButton $btn) => $btn->class('my-class'))
;
}
}
If the import() method returns NULL, the import button will not be displayed on the index page.
To change the import logic, you can use the model resource events.
public function beforeImportFilling(array $data): array
{
return $data;
}
public function beforeImported(mixed $item): mixed
{
return $item;
}
public function afterImported(mixed $item): mixed
{
return $item;
}
public function beforeImportFilling(array $data): array
{
return $data;
}
public function beforeImported(mixed $item): mixed
{
return $item;
}
public function afterImported(mixed $item): mixed
{
return $item;
}
namespace App\MoonShine\Resources;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\UI\Fields\ID;
use MoonShine\UI\Fields\Text;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
protected function exportFields(): iterable
{
return [
ID::make(),
Text::make('Name'),
];
}
}
If you need to modify a value during export, you should use the field method modifyRawValue().
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:8]
namespace App\MoonShine\Resources;
use App\Enums\StatusEnum;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\UI\Fields\Enum;
use MoonShine\UI\Fields\ID;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
protected function exportFields(): iterable
{
return [
ID::make(),
Enum::make('Status')
->attach(StatusEnum::class)
->modifyRawValue(static fn(StatusEnum $raw, Enum $ctx) => $raw->value),
];
}
}
namespace App\MoonShine\Resources;
use App\Enums\StatusEnum;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\UI\Fields\Enum;
use MoonShine\UI\Fields\ID;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
protected function exportFields(): iterable
{
return [
ID::make(),
Enum::make('Status')
->attach(StatusEnum::class)
->modifyRawValue(static fn(StatusEnum $raw, Enum $ctx) => $raw->value),
];
}
}
For configuring the export, optional methods are available.
To use them, you need to add the export() method that returns ExportHandler.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:8]
namespace App\MoonShine\Resources;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\ExportHandler;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Handlers\Handler;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\UI\Components\ActionButton;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
protected function export(): ?Handler
{
return ExportHandler::make(__('moonshine::ui.export'))
// Specify the IDs of users who will be notified when the operation is completed
->notifyUsers(fn() => [auth()->id()])
// Select disk
->disk('public')
// Filename
->filename(sprintf('export_%s', date('Ymd-His')))
// Select directory for saving the export file
->dir('/exports')
// If you need to export in csv format
->csv()
// CSV delimiter
->delimiter(',')
// Export with confirmation
->withConfirm()
// Modify button
->modifyButton(fn(ActionButton $btn) => $btn->class('my-class'))
;
}
}
namespace App\MoonShine\Resources;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\ExportHandler;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Handlers\Handler;
use MoonShine\Laravel\Resources\ModelResource;
use MoonShine\UI\Components\ActionButton;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
protected function export(): ?Handler
{
return ExportHandler::make(__('moonshine::ui.export'))
// Specify the IDs of users who will be notified when the operation is completed
->notifyUsers(fn() => [auth()->id()])
// Select disk
->disk('public')
// Filename
->filename(sprintf('export_%s', date('Ymd-His')))
// Select directory for saving the export file
->dir('/exports')
// If you need to export in csv format
->csv()
// CSV delimiter
->delimiter(',')
// Export with confirmation
->withConfirm()
// Modify button
->modifyButton(fn(ActionButton $btn) => $btn->class('my-class'))
;
}
}
If the export() method returns NULL, the export button will not be displayed on the index page.
$value - condition,
$callback - callback function that will be executed if the condition is TRUE,
$default - callback function that will be executed if the condition is FALSE.
// torchlight! {"summaryCollapsedIndicator": "namespaces"}
// [tl! collapse:7]
namespace App\MoonShine\Resources;
use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\ImportHandler;
use MoonShine\ImportExport\Traits\ImportExportConcern;
use MoonShine\Laravel\Handlers\Handler;
use MoonShine\Laravel\Resources\ModelResource;
class CategoryResource extends ModelResource implements HasImportExportContract
{
use ImportExportConcern;
// ...
protected function import(): ?Handler
{
return ImportHandler::make('Import')
->when(
true,
fn($handler) => $handler->delimiter(','),
fn($handler) => $handler->delimiter(';')
);
}
}
$value - condition,
$callback - callback function that will be executed if the condition is FALSE,
$default - callback function that will be executed if the condition is TRUE.
The unless() method is the reverse of the when() method.
There may be situations where you want to change the import or export implementation. To do this, you need to implement your own class extending ImportHandler or ExportHandler.
You can generate the class by using the console command:
php artisan moonshine:handler
phpartisanmoonshine:handler
php artisan moonshine:handler
After running the command, a base class Handler will be created in the app/MoonShine/Handlers directory.
Don’t forget to change the inheritance from Handler to ImportHandler or ExportHandler.