camya/filament-import-inline
Installation:
composer require camya/filament-import-inline
Publish the config (if needed):
php artisan vendor:publish --provider="Camya\FilamentImportInline\FilamentImportInlineServiceProvider"
First Use Case:
Add the ImportInlineInput to a Filament resource form:
use Camya\FilamentImportInline\Forms\Components\ImportInlineInput;
public static function form(Form $form): Form
{
return $form
->schema([
ImportInlineInput::make('import_data')
->jsonString() // Default importer (JSON)
->label('Paste JSON/CSV Data'),
]);
}
Quick Test:
[{"name":"John","email":"john@example.com"}] or "name,email\nJohn,john@example.com").JSON Import:
ImportInlineInput::make('users')
->jsonString()
->validationRules(['required', 'array'])
->columnMapping([
'name' => 'Name',
'email' => 'Email',
]);
columnMapping to rename keys for clarity in the UI.CSV Import:
ImportInlineInput::make('products')
->csvString()
->validationRules(['required', 'string'])
->separator(',')
->enclosure('"');
separator/enclosure for non-standard CSV formats.Custom Importers:
ImportInlineInput::make('custom_data')
->importer(function (string $string) {
return explode("\n", $string); // Simple newline-based importer
})
->validationRules(['required', 'array']);
Form Submission Handling:
->validationRules() to enforce structure:
->validationRules(['required', 'array', 'min:1'])
->rules() in the resource’s create()/update() methods.Dynamic Column Mapping: Dynamically map CSV/JSON keys to model attributes:
->columnMapping(function (array $data) {
return collect($data)->first() // Infer from first row
->mapWithKeys(fn ($value, $key) => [$key => $key]);
});
Livewire Integration:
wire:model for reactive updates if needed:
->wireModel('imported_data')
Error Handling:
rules():
public static function rules(): array
{
return [
'import_data' => ['required', 'array'],
'import_data.*' => ['required', 'string'],
];
}
Validation Timing:
validationRules() matches the expected structure (e.g., array for JSON objects).CSV Edge Cases:
->enclosure('"') for safety."name,title").Data Overwrite:
->default(fn () => $this->record?->import_data ?? [])
Performance:
->debounce(500) to throttle processing:
->debounce(500) // Delay in ms
Log Importer Output: Add a temporary importer to debug:
->importer(function (string $string) {
\Log::debug('Raw input:', [$string]);
return json_decode($string, true);
})
Validation Errors:
storage/logs/laravel.log) for validation failures.->validationAttribute('Imported Data') for clearer error labels.Custom Importer Quirks:
array or object).->importer(function (string $string) {
return is_array(json_decode($string, true)) ? json_decode($string, true) : [];
})
Custom UI Feedback: Override the default success/error messages:
->successMessage('Data imported successfully!')
->errorMessage('Invalid data format. Please check your input.')
Pre-Import Processing:
Use ->beforeImport() to transform data before validation:
->beforeImport(function (array $data) {
return array_map('strtolower', $data);
})
Post-Import Actions: Trigger actions after import (e.g., dispatch events):
->afterImport(function (array $data) {
event(new DataImported($data));
})
Localization: Translate messages via Filament’s language files:
->successMessage(__('filament-import-inline::messages.success'))
How can I help you explore Laravel packages today?