Skip to content

abduromanov/filament-import

 
 

Repository files navigation

Screenshot of Login

Filament Plugin for Import CSV and XLS into Database

FILAMENT 2.x Packagist Downloads

Code Styles run-tests

This package will make it easier for you to import from files to your model, very easily without the need to do templates.

all you have to do is drag and drop and match the fields and columns of your file, and let magic happens!

Installation

You can install the package via composer:

composer require konnco/filament-import

Publishing Config

If you want to do the settings manually, please publish the existing config.

php artisan vendor:publish --tag=filament-import-config

Usage

import the actions into ListRecords page

use Konnco\FilamentImport\Actions\ImportAction;
use Konnco\FilamentImport\ImportField;

class ListCredentialDatabases extends ListRecords
{
    protected static string $resource = CredentialDatabaseResource::class;

    protected function getActions(): array
    {
        return [
            ImportAction::make()
                ->fields([
                    ImportField::make('project')
                        ->label('Project')
                        ->helperText('Define as project helper'),
                    ImportField::make('manager')
                        ->label('Manager'),
                ])
        ];
    }
}

Required Field

protected function getActions(): array
{
    return [
        ImportAction::make()
            ->fields([
                ImportField::make('project')
                    ->label('Project')
                    ->required(),
            ])
    ];
}

Disable Mass Create

if you still want to stick with the event model you might need this and turn off mass create

protected function getActions(): array
{
    return [
        ImportAction::make()
            ->massCreate(false)
            ->fields([
                ImportField::make('project')
                    ->label('Project')
                    ->required(),
            ])
    ];
}

Mutate Data

you can also manipulate data from row spreadsheet before saving to model

protected function getActions(): array
{
    return [
        ImportAction::make()
            ->fields([
                ImportField::make('project')
                    ->label('Project')
                    ->mutateBeforeCreate(fn($value) => Str::of($value)->camelCase())
                    ->required(),
            ])
    ];
}

otherwise you can manipulate data with other column in the same row by passing $row as second argument and access the index of column

For example when create email from username

protected function getActions(): array
{
    return [
        ImportAction::make()
            ->fields([
                ImportField::make('email')
                    ->label('Email')
                    ->mutateBeforeCreate(fn($value, $row) => $row['username'] . '@mail.com')
                    ->required(),
            ])
    ];
}

Grid Column

Of course, you can divide the column grid into several parts to beautify the appearance of the data map

protected function getActions(): array
{
    return [
        ImportAction::make()
            ->fields([
                ImportField::make('project')
                    ->label('Project')
                    ->required(),
            ], columns:2)
    ];
}

Json Format Field

We also support the json format field, which you can set when calling the make function and separate the name with a dot annotation

protected function getActions(): array
{
    return [
        ImportAction::make()
            ->fields([
                ImportField::make('project.en')
                    ->label('Project In English')
                    ->required(),
                ImportField::make('project.id')
                    ->label('Project in Indonesia')
                    ->required(),
            ], columns:2)
    ];
}

Static Field Data

for the static field data you can use the common fields from filament

use Filament\Forms\Components\Select;

protected function getActions(): array
{
    return [
        ImportAction::make()
            ->fields([
                ImportField::make('name')
                    ->label('Project')
                    ->required(),
                Select::make('status')
                    ->options([
                        'draft' => 'Draft',
                        'reviewing' => 'Reviewing',
                        'published' => 'Published',
                    ])
            ], columns:2)
    ];
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Collaborators

abduromanov
Hafiz Abd

Contributors

frankyso
Franky So
abduromanov
Hafiz Abd
rizkyanfasafm
Rizky Anfasa Farras Mada
tryoasnafi
Tryo Asnafi

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%