Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

juanborras's avatar

filament ImportColumn, temp field

I'm trying to import a student csv file where a required field (unit) for the import process is required, but it is not required to be imported into the db (in fact it is not mapped). The import process fails because the $this->record ends up with the 'unit' key... any ideas?

Thanks!

0 likes
2 replies
LaryAI's avatar
Level 58

To handle the scenario where a field is required during the import process but not needed in the database, you can use a temporary field in your Filament resource. This allows you to validate and manipulate the data during the import process without affecting your database structure.

Here's a step-by-step solution to manage the 'unit' field as a temporary field during the import process:

  1. Define a Temporary Field in Your Import Resource: You can define a temporary field in your import resource that is not mapped to any database column.

  2. Validation and Cleanup: After validation, you can remove this temporary field before saving the record to the database.

Here's an example of how you might implement this:

use Filament\Resources\Form;
use Filament\Resources\Tables;
use Filament\Resources\Resource;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Columns\TextColumn;

class StudentResource extends Resource
{
    // Define the model associated with the resource
    public static function model()
    {
        return Student::class;
    }

    // Define the form used for creating and editing resources
    public static function form(Form $form)
    {
        return $form->schema([
            TextInput::make('name')->required(),
            TextInput::make('unit')->required()->columnSpan(1),
            // other fields...
        ]);
    }

    // Define the table used for listing resources
    public static function table(Tables\Table $table)
    {
        return $table->columns([
            TextColumn::make('name'),
            TextColumn::make('unit'),
            // other columns...
        ]);
    }

    // Define the import process
    public static function importRecord($record, $columnMapping)
    {
        // Perform any necessary transformation or validation
        $unit = $record['unit'];

        // You might want to do something with 'unit' here, like validation

        // Remove the 'unit' field since it's not needed in the database
        unset($record['unit']);

        // Create a new student record without the 'unit'
        return static::model()::create($record);
    }
}

In this example, the importRecord method is used to handle the import logic. It takes the record being imported and the column mapping as parameters. The 'unit' field is accessed and can be validated or transformed as needed. Before creating the new student record, the 'unit' field is removed from the $record array since it's not needed in the database.

This approach allows you to handle temporary fields that are necessary for validation or processing during import but do not need to be stored in your database.

juanborras's avatar

Thanks!

Found easier solution

protected function beforeSave(): void
    {
        unset($this->record['unit']);
    }
````

Please or to participate in this conversation.