PHP code example of romansulzhyk / filament-import

1. Go to this page and download the library: Download romansulzhyk/filament-import 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/ */

    

romansulzhyk / filament-import example snippets


use RomanSulzhyk\FilamentImport\Actions\ExcelImportAction;

class ListCustomers extends ListRecords
{
    protected function getHeaderActions(): array
    {
        return [
            ExcelImportAction::make(),
        ];
    }
}

ExcelImportAction::make()
    ->importColumns(['name', 'email', 'phone'])   // only these, and only if fillable
    ->exceptColumns(['role', 'is_admin']);         // never these

class ContactImporter extends Importer
{
    // ...columns...

    protected function beforeSave(): void
    {
        $this->record->team()->associate(Filament::getTenant());
    }
}

ExcelImportAction::make()->importer(ContactImporter::class);

ExcelImportAction::make()
    ->validateUsing([
        'name' => 'mail');

ExcelImportAction::make()
    ->importer(CustomerImporter::class);

ExcelImportAction::make()
    ->beforeImport(function (array $data, $livewire, $excelImportAction) {
        // Runs before any row is written. Call $excelImportAction->halt() to stop.
    })
    ->afterImport(function (array $data, $livewire, $excelImportAction, ImportResult $result) {
        // $result->created, ->updated, ->skipped, ->failed(), ->failures
    })
    ->importerOptions(['send_welcome_email' => false]) // Available to the importer as $this->options
    ->optionsFormComponents([
        Select::make('source')->options(['fair' => 'Trade fair', 'web' => 'Website'])->