PHP code example of moonshine / import-export

1. Go to this page and download the library: Download moonshine/import-export library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

moonshine / import-export example snippets


use MoonShine\ImportExport\Contracts\HasImportExportContract;
use MoonShine\ImportExport\Traits\ImportExportConcern;

class CategoryResource extends ModelResource implements HasImportExportContract
{
    use ImportExportConcern;
    
    // ...
}

protected function exportFields(): iterable
{
    return [
        ID::make(),
        Position::make(),
        Text::make('Name'),
    ];
}

protected function importFields(): iterable
{
    return [
        ID::make(),
        Text::make('Name'),
    ];
}

public function beforeImportFilling(array $data): array
{
    return $data;
}

public function beforeImported(mixed $item): mixed
{
    return $item;
}

public function afterImported(mixed $item): mixed
{
    return $item;
}

protected function export(): ?Handler
{
    return ExportHandler::make(__('moonshine::ui.export'))
        ->when(
            $this->isExportToCsv(),
            static fn (ExportHandler $handler): ExportHandler => $handler->csv()
        )
        ->queue();
}

protected function import(): ?Handler
{
    return ImportHandler::make(__('moonshine::ui.import'))->queue();
}