Skip to content

moonshine-software/import-export

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MoonShine Import/Export Handlers

Documentation

Requirements

  • MoonShine 3+
  • Laravel 10+
  • PHP 8.2+

Installation

composer require moonshine/import-export

Usage

  • Add trait MoonShine\ImportExport\Traits\ImportExportConcern and interface MoonShine\ImportExport\Contracts\HasImportExportContract to ModelResource
/**
 * @extends ModelResource<Category>
 */
class CategoryResource extends ModelResource implements HasImportExportContract
{
    use ImportExportConcern;
    
    // ...
}
  • Define fields
/**
 * @extends ModelResource<Category>
 */
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'),
        ];
    }
}
  • 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;
}