Import / Export

# Basics

By default, import and export are enabled for all model resources.
To override this behavior, you need to change the moonshine configuration.

// config/moonshine.php
 
'model_resources' => [
'default_with_import' => false,
'default_with_export' => false,
],

You can also disable import and export in a resource; for this, the corresponding methods must return null.

namespace App\MoonShine\Resources;
 
use App\Models\Post;
use MoonShine\Resources\ModelResource;
 
class PostResource extends ModelResource
{
protected string $model = Post::class;
 
protected string $title = 'Posts';
 
public function import(): ?ImportHandler
{
return null;
}
 
public function export(): ?ExportHandler
{
return null;
}
 
//...
}

# Import

You can import data into the MoonShine admin panel.

Fields

The fields that will participate in the import are required in the model resource, add useOnImport() method.

useOnImport(mixed $condition = null)
namespace App\MoonShine\Resources;
 
use App\Models\Post;
use MoonShine\Fields\ID;
use MoonShine\Fields\Text;
use MoonShine\Resources\ModelResource;
 
class PostResource extends ModelResource
{
protected string $model = Post::class;
 
protected string $title = 'Posts';
 
//...
 
public function fields(): array
{
return [
ID::make()
->useOnImport(),
 
Text::make('Title', 'title')
->useOnImport()
];
}
 
//...
}

Be sure to add ID to the import, otherwise, records will only be added without updating existing ones.

Handler

It is also necessary to implement the import() method in the model resource. The method must return an ImportHandler object that implements the data import algorithm.

namespace App\MoonShine\Resources;
 
use App\Models\Post;
use MoonShine\Handlers\ImportHandler;
use MoonShine\Resources\ModelResource;
 
class PostResource extends ModelResource
{
protected string $model = Post::class;
 
protected string $title = 'Posts';
 
//...
 
public function import(): ?ImportHandler
{
return ImportHandler::make('Import');
}
 
//...
}
Methods ImportHandler

Optional methods are available to configure import:

use MoonShine\Handlers\ImportHandler;
 
//...
 
public function import(): ?ImportHandler
{
return ImportHandler::make('Import')
// Disc selection
->disk('public')
// Selecting a directory to save the import file
->dir('/imports')
// Delete file after import
->deleteAfter()
// Separator for csv
->delimiter(',');
}
 
//...

If the import() method returns NULL, then the import button will not appear on the index page.

# Export

In the MoonShine admin panel you can export all data taking into account the current filtering and sorting.

By default, data is exported in xlsx format, but there is an option to export in csv format.

Fields

Only those fields that have the showOnExport() method added will participate in the export.

showOnExport(mixed $condition = null)
namespace App\MoonShine\Resources;
 
use App\Models\Post;
use MoonShine\Fields\Text;
use MoonShine\Resources\ModelResource;
 
class PostResource extends ModelResource
{
protected string $model = Post::class;
 
protected string $title = 'Posts';
 
//...
 
public function fields(): array
{
return [
Text::make('Title', 'title')
->showOnExport()
];
}
 
//...
}
Handler

It is also necessary to implement the export() method in the model resource. The method must return an ExportHandler object that implements the data export algorithm.

namespace App\MoonShine\Resources;
 
use App\Models\Post;
use MoonShine\Handlers\ExportHandler;
use MoonShine\Resources\ModelResource;
 
class PostResource extends ModelResource
{
protected string $model = Post::class;
 
protected string $title = 'Posts';
 
//...
 
public function export(): ?ExportHandler
{
return ExportHandler::make('Export');
}
 
//...
}
Methods ExportHandler

Optional methods are available to configure export:

use MoonShine\Handlers\ExportHandler;
 
//...
 
public function export(): ?ExportHandler
{
return ExportHandler::make('Export')
// Disc selection
->disk('public')
// Selecting the directory for saving the export file
->dir('/exports')
// If you need to export in csv format
->csv()
// Separator for csv
->delimiter(',');
}
 
//...

If the export() method returns NULL, then the export button will not appear on the index page.

# Handler methods

ImportHandler and ExportHandler extend the base class Handler that implements additional methods.

icon
// icon for button
icon(string $icon)
queue
// run processes in the background
queue()
when
// conditional methods
when($value = null, callable $callback = null, callable $default = null)

$value - condition,
$callback - callback function that will be executed if the condition is true TRUE,
$default - callback function that will be executed if the condition is true FALSE.

use MoonShine\Handlers\ImportHandler;
 
//...
 
public function import(): ?ImportHandler
{
return ImportHandler::make('Import')
->when(
true,
fn($handler) => $handler->delimiter(','),
fn($handler) => $handler->delimiter(';')
);
}
 
//...
unless
// conditional methods
unless($value = null, callable $callback = null, callable $default = null)

$value - condition,
$callback - callback function that will be executed if the condition is true FALSE,
$default - callback function that will be executed if the condition is true TRUE.

The unless() method is the when() method the inverse.

# Custom implementation

There may be a situation where you want to change your import or export implementation. To do this, you need to implement your own class extending ImportHandler or ExportHandler.

The class can be generated using the console command:

php artisan moonshine:handler

After executing the command, a base handler class will be created in the app/MoonShine/Handlers directory.