Import validation

Hi all, I have a simple importer, only the sheet that i want to import hase some columns with NULL as string How can i cast or save it as null in database? Both options below wont work and throws me a validation error that The from email field is required. I want the value to be null if string of state == 'NULL'
Imports\ImportColumn::make('from_email')
->ignoreBlankState()
->castStateUsing(function (?string $state): ?string {
if (blank($state) or (\Str::upper($state) == 'NULL')) {
return null;
}

return $state;
})

Imports\ImportColumn::make('from_email')
->ignoreBlankState()
->fillRecordUsing(function ($record, string $state): void {
if(\Str::upper($state) == 'NULL'){
$record->from_email = null;
}
$record->from_email = $state;
})
Imports\ImportColumn::make('from_email')
->ignoreBlankState()
->castStateUsing(function (?string $state): ?string {
if (blank($state) or (\Str::upper($state) == 'NULL')) {
return null;
}

return $state;
})

Imports\ImportColumn::make('from_email')
->ignoreBlankState()
->fillRecordUsing(function ($record, string $state): void {
if(\Str::upper($state) == 'NULL'){
$record->from_email = null;
}
$record->from_email = $state;
})
My migration has the field als nullable
$table->text('from_email')->nullable();
$table->text('from_email')->nullable();
Also i dont see where the validation would be happening because there is no validation on the fields. Thanks for the help!
6 Replies
Tieme
TiemeOP7d ago
up
christmex
christmex7d ago
u want save null as string in database? or null exacly?
ImportColumn::make('from_email')
ImportColumn::make('from_email')
wht about tht? if the csv u import doenst hve value or null, it woll ignore it if im not wrong,
Tieme
TiemeOP7d ago
The import has "NULL" as string, and in database i want it to be null so nothing in DB if from_email = 'NULL' if i only use
php ImportColumn::make('from_email')
php ImportColumn::make('from_email')
than in DB there will be a string "NULL"
Bruno Pereira
Bruno Pereira7d ago
Imports\ImportColumn::make('from_email')
->castStateUsing(function (?string $state): ?string {
if (blank($state) || (\Str::upper($state) === 'NULL')) {
return null;
}

return $state;
})
Imports\ImportColumn::make('from_email')
->castStateUsing(function (?string $state): ?string {
if (blank($state) || (\Str::upper($state) === 'NULL')) {
return null;
}

return $state;
})
Try this
Tudor
Tudor6d ago
I get the same error...and im looking into it for over 2h now..cant find a solution this is not working, I still get "validation.required" error
Bruno Pereira
Bruno Pereira6d ago
try requiring mapping
Imports\ImportColumn::make('from_email')
->requiredMapping()
->castStateUsing(function (?string $state): ?string {
if (blank($state) || (\Str::upper($state) === 'NULL')) {
return null;
}

return $state;
})
Imports\ImportColumn::make('from_email')
->requiredMapping()
->castStateUsing(function (?string $state): ?string {
if (blank($state) || (\Str::upper($state) === 'NULL')) {
return null;
}

return $state;
})
Can you show the model and resource code by any chance?

Did you find this page helpful?