This Filament Plugin will enable you to import files to upsert models by matching columns via regex.
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"
Matches given mappers' regex with model columns
Username | |
---|---|
Sebastian | sebastian@walbrun-consulting.de |
John | john@doe.test |
public function propertyMapping(): Collection
{
return collect([
'name' => '/(user|first|last)?name)/i',
'email' => '/(E(-|_)?)?Mail/i',
]);
}
Fails in case two regexes are matching the same column.
Creates or updates models taking care of the given unique columns
public function uniqueColumns(): array
{
return [
'email',
];
}
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();
}
};
]);
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.