i'm using Fiilamentphp for my laravel project, i'm using "filament/filament": "^3.0-stable", and "konnco/filament-import": "^1.6".
I want to add 'created_by' = Auth::id();
to be executed before saving each record.
the code TestImporter.php:
<?php
namespace App\Filament\Imports;
use App\Models\Voucher;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
class TestImporter extends Importer
{
protected static ?string $model = Test::class;
public static function getColumns(): array
{
return [
ImportColumn::make('test_column')
->label('Test Column')
->requiredMapping(),
ImportColumn::make('product_id')
->label('Product Name')
->relationship('product', 'name')
->requiredMapping()
->example('SUV'),
];
}
public function resolveRecord(): ?Test
{
return new Test();
}
public static function getCompletedNotificationBody(Import $import): string
{
$body = 'Your test import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.';
if ($failedRowsCount = $import->getFailedRowsCount()) {
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.';
}
return $body;
}
}
How do I add it? I tried adding it to the resolveRecord() and it's not working.
I found a solution but not really convinced by it
public function resolveRecord(): ?Voucher
{
return new Voucher([
'serial_voucher' => $this->data['serial_voucher'],
'product_id' => $this->data['product_id'],
'created_by' => Auth::id(),
]);
}
This made the record to be saved, so the problem solved.