Import action, unsetting columns not working

I am using the import functionality, but I am facing some problems with the Import columns I need to add 2 columns that are not related with the model because I have some extra logic in the beforeSave that i want to implement but I am getting error:
[2024-11-11 11:54:13] local.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'start_year' in 'field list' (Connection: mysql, SQL: insert into `codes`
[2024-11-11 11:54:13] local.ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'start_year' in 'field list' (Connection: mysql, SQL: insert into `codes`
I thought that with the unset I would be able to fix it, but it seems I was wrong.
public function beforeSave(): void
{
// do some extra logic here with start and stop year columns


// Remove start_year and stop_year from $this->data
unset($this->data['start_year'], $this->data['stop_year']);

}
public function beforeSave(): void
{
// do some extra logic here with start and stop year columns


// Remove start_year and stop_year from $this->data
unset($this->data['start_year'], $this->data['stop_year']);

}
These are the columns:
public static function getColumns(): array
{
return [
ImportColumn::make('code')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('brand')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('part')
->requiredMapping()
->relationship()
->rules(['required']),
ImportColumn::make('start_year')
->label('Start Year')
->rules(['nullable', 'max:4']), // Optional since it's only for CarModel
ImportColumn::make('stop_year')
->label('Stop Year')
->rules(['nullable', 'max:4']), // Optional since it's only for CarModel
];
}
public static function getColumns(): array
{
return [
ImportColumn::make('code')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('brand')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('part')
->requiredMapping()
->relationship()
->rules(['required']),
ImportColumn::make('start_year')
->label('Start Year')
->rules(['nullable', 'max:4']), // Optional since it's only for CarModel
ImportColumn::make('stop_year')
->label('Stop Year')
->rules(['nullable', 'max:4']), // Optional since it's only for CarModel
];
}
1 Reply
toeknee
toeknee2w ago
try:
public function beforeSave(): void
{
// Make a copy of $data
$data = $this->data;

// Remove the start_year and stop_year fields from $data
unset($data['start_year'], $data['stop_year']);

// Reassign the modified data back to $this->data
$this->data = $data;
}
public function beforeSave(): void
{
// Make a copy of $data
$data = $this->data;

// Remove the start_year and stop_year fields from $data
unset($data['start_year'], $data['stop_year']);

// Reassign the modified data back to $this->data
$this->data = $data;
}
Want results from more Discord servers?
Add your server