swalbrun/filament-regex-import

This Filament Plugin will enable you to import files to upsert models by matching columns via regex.

0.2.1 2023-06-08 13:22 UTC

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

This Filament Plugin will enable you to import files to upsert models by matching columns via regex.

Page

Modal

Installation

You can install the package via composer:

composer require swalbrun/filament-regex-import

Create a mapper using the make command

php artisan filament:make-filament-import-mapper UserMapper

You can publish the config file with:

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

This is the contents of the published config file:

return [
    'accepted_mimes' => [
        'application/vnd.ms-excel',
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'text/csv',
        'text/plain',
        'csv',
    ],
    'mappers' => [
    ],
    'navigation_group' => 'Import',
];

You can publish the translation file with:

php artisan vendor:publish --tag="filament-regex-import-translations"

Features

Matching header columns with configured regex

Matches given mappers' regex with model columns

 public function propertyMapping(): Collection
    {
        return collect([
            'name' => '/(user|first|last)?name)/i',
            'email' => '/(E(-|_)?)?Mail/i',
        ]);
    }

Detecting overlapping regexes

Fails in case two regexes are matching the same column.

Upserting models via unique keys

Creates or updates models taking care of the given unique columns

public function uniqueColumns(): array
    {
        return [
            'email',
        ];
    }

Relating models

Call hooks for relating found models. The hooks will get called in case all hooks arguments models have been found

public function relatingClosures(): Collection
    {
        return collect([
            fn (User $user, Role $role) => $user->roles()->saveMany([$role]),
            fn (User $user) => event(new UserImported($user)),
            // Only gets called if a user, role and post with the matching type has been found by import
            function (User $user, Role $role, Post $post)  {
                if ($role->is('user')) {
                    $user->post()->associate($post)->save();
                }
            };
        ]);
    }

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.

Credits

License

The MIT License (MIT). Please see License File for more information.