Import action make field nullable does not work

I want to store fields in db even if they are not valid or empty as null, but I can't get this to work. I have tried to cast the field to null but it always says "must be valid date".
return [
ImportColumn::make('salutation')
->rules(['max:255']),
ImportColumn::make('firstname')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('lastname')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('email')
->rules(['nullable', 'max:255']),
ImportColumn::make('phone')
->rules(['max:255']),
ImportColumn::make('whatsapp_number')
->rules(['max:255']),
ImportColumn::make('follow_up')
->castStateUsing(fn($state) => $state ? Carbon::parse($state) : null) // here for example
->rules(['max:255']),
ImportColumn::make('notes')
->rules(['max:10000']),
];
return [
ImportColumn::make('salutation')
->rules(['max:255']),
ImportColumn::make('firstname')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('lastname')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('email')
->rules(['nullable', 'max:255']),
ImportColumn::make('phone')
->rules(['max:255']),
ImportColumn::make('whatsapp_number')
->rules(['max:255']),
ImportColumn::make('follow_up')
->castStateUsing(fn($state) => $state ? Carbon::parse($state) : null) // here for example
->rules(['max:255']),
ImportColumn::make('notes')
->rules(['max:10000']),
];
Help is much appreciated! 🙂
Solution:
Thanks solved it like so ``` protected function beforeValidate(): void {...
Jump to solution
24 Replies
toeknee
toeknee7mo ago
You need to set your column for date to be nullable in the db.
kekitek
kekitekOP7mo ago
it is already $table->timestamp('follow_up')->nullable();
toeknee
toeknee7mo ago
email has nothing to do with a valid date? which I suspect is follow_up
kekitek
kekitekOP7mo ago
Sorry I was copying the wrong line of code. The problem is still the same. Updated the post above
Dennis Koch
Dennis Koch7mo ago
I have tried to cast the field to null but it always says "must be valid date".
Is that an exception or a validation message?
kekitek
kekitekOP7mo ago
This is a validation message I see in the document at the end of an row
kekitek
kekitekOP7mo ago
No matter what I always get validation errors, struggling for days now.
No description
toeknee
toeknee7mo ago
Provide your test CSV
kekitek
kekitekOP7mo ago
Can't provide the whole file, but I want not valid data to be stored as null
No description
No description
toeknee
toeknee7mo ago
You need to remove not valid data then and just cast it.
kekitek
kekitekOP7mo ago
So you mean I should only provide valid data in my files?
toeknee
toeknee7mo ago
No, you can cast it https://filamentphp.com/docs/3.x/actions/prebuilt-actions/import#casting-state which adjusts that it will be, so casting to null if invalid will work if you allow nulls in that column.
kekitek
kekitekOP7mo ago
Tried that already, but no success
$table->timestamp('follow_up')->nullable();
$table->timestamp('follow_up')->nullable();
->castStateUsing(function ($value) {
try {
$date = new \DateTime($value);
return $date->getTimestamp();
} catch (\Exception $e) {
return null;
}
}),
->castStateUsing(function ($value) {
try {
$date = new \DateTime($value);
return $date->getTimestamp();
} catch (\Exception $e) {
return null;
}
}),
toeknee
toeknee7mo ago
And what is the state? is the state null? If it iss then your not allow to store nulls. Remove the rules too
kekitek
kekitekOP7mo ago
Did that already
ImportColumn::make('follow_up')
->castStateUsing(function ($value) {
try {
$date = new \DateTime($value);
return $date->getTimestamp();
} catch (\Exception $e) {
return null;
}
}),
ImportColumn::make('follow_up')
->castStateUsing(function ($value) {
try {
$date = new \DateTime($value);
return $date->getTimestamp();
} catch (\Exception $e) {
return null;
}
}),
And what is the state? is the state null? If it iss then your not allow to store nulls.
And what is the state? is the state null? If it iss then your not allow to store nulls.
How to change that?
toeknee
toeknee7mo ago
try { $date = new \DateTime($value); $newState = $date->getTimestamp(); } catch (\Exception $e) { $newState = null; } dd($newState);
kekitek
kekitekOP7mo ago
ok does nothing
toeknee
toeknee7mo ago
Interesting, try logging it instead. The importer could be catching the exception Logging on the try and the catch
kekitek
kekitekOP7mo ago
Ok I get nothing in my log
ImportColumn::make('follow_up')
->castStateUsing(function ($value) {
try {
$date = new \DateTime($value);
$newState = $date->getTimestamp();
} catch (\Exception $e) {
$newState = null;
Log::info('Follow up date: ' . $newState);
}
}),
ImportColumn::make('follow_up')
->castStateUsing(function ($value) {
try {
$date = new \DateTime($value);
$newState = $date->getTimestamp();
} catch (\Exception $e) {
$newState = null;
Log::info('Follow up date: ' . $newState);
}
}),
same here
try {
$date = new \DateTime($value);
$newState = $date->getTimestamp();
Log::info('Follow up date: ' . $newState);
} catch (\Exception $e) {
$newState = null;
Log::info('Follow up date: ' . $newState);
}
try {
$date = new \DateTime($value);
$newState = $date->getTimestamp();
Log::info('Follow up date: ' . $newState);
} catch (\Exception $e) {
$newState = null;
Log::info('Follow up date: ' . $newState);
}
Is there someone else who has an idea how to handle this? Please really need this
LeandroFerreira
LeandroFerreira7mo ago
maybe using a beforeValidate? $this->data['follow_up'] = $this->data['follow_up'] ? Carbon::parse($this->data['follow_up']) : null https://filamentphp.com/docs/3.x/actions/prebuilt-actions/import#lifecycle-hooks
kekitek
kekitekOP7mo ago
Thanks I will try this 🙂
LeandroFerreira
LeandroFerreira7mo ago
If it doesn't work, provide a mini repo on Github to reproduce this issue.. I can take a look
kekitek
kekitekOP7mo ago
Oh wow thanks for your effort Will check it later no time at the moment
Solution
kekitek
kekitek7mo ago
Thanks solved it like so
protected function beforeValidate(): void
{
$this->data['follow_up'] = $this->parseFollowUp($this->data['follow_up']);
}

/**
* Parse the follow up date.
*/
protected function parseFollowUp($followUp)
{
if (empty($followUp)) {
return null;
}

try {
return Carbon::parse($followUp)->getTimestamp();
} catch (\Exception $e) {
return null;
}
}
protected function beforeValidate(): void
{
$this->data['follow_up'] = $this->parseFollowUp($this->data['follow_up']);
}

/**
* Parse the follow up date.
*/
protected function parseFollowUp($followUp)
{
if (empty($followUp)) {
return null;
}

try {
return Carbon::parse($followUp)->getTimestamp();
} catch (\Exception $e) {
return null;
}
}
Want results from more Discord servers?
Add your server