swalbrun / filament-regex-import
This Filament Plugin will enable you to import files to upsert models by matching columns via regex.
Fund package maintenance!
sWalbrun
Requires
- php: ^8.0
- filament/filament: ^2.17
- illuminate/contracts: ^9.0|^10.0
- maatwebsite/excel: ^3.1
- spatie/laravel-package-tools: ^1.13.5
Requires (Dev)
- captainhook/captainhook: ^5.16
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- pestphp/pest-plugin-livewire: ^1.0
- pestphp/pest-plugin-parallel: ^1.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- ramsey/conventional-commits: ^1.5
- spatie/laravel-permission: ^5.10
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-11-09 06:11:09 UTC
README
This Filament Plugin will enable you to import files to upsert models by matching columns via regex.
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.