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
toeknee3w ago
You need to set your column for date to be nullable in the db.
kekitek
kekitek3w ago
it is already $table->timestamp('follow_up')->nullable();
toeknee
toeknee3w ago
email has nothing to do with a valid date? which I suspect is follow_up
kekitek
kekitek3w ago
Sorry I was copying the wrong line of code. The problem is still the same. Updated the post above
Dennis Koch
Dennis Koch3w 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
kekitek3w ago
This is a validation message I see in the document at the end of an row
kekitek
kekitek3w ago
No matter what I always get validation errors, struggling for days now.
No description
toeknee
toeknee3w ago
Provide your test CSV
kekitek
kekitek3w ago
Can't provide the whole file, but I want not valid data to be stored as null
No description
No description
toeknee
toeknee3w ago
You need to remove not valid data then and just cast it.
kekitek
kekitek3w ago
So you mean I should only provide valid data in my files?
toeknee
toeknee3w 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
kekitek3w 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
toeknee3w 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
kekitek3w 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
toeknee3w ago
try { $date = new \DateTime($value); $newState = $date->getTimestamp(); } catch (\Exception $e) { $newState = null; } dd($newState);
kekitek
kekitek3w ago
ok does nothing
toeknee
toeknee3w ago
Interesting, try logging it instead. The importer could be catching the exception Logging on the try and the catch
kekitek
kekitek3w 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
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
kekitek3w ago
Thanks I will try this 🙂
LeandroFerreira
If it doesn't work, provide a mini repo on Github to reproduce this issue.. I can take a look
kekitek
kekitek3w ago
Oh wow thanks for your effort Will check it later no time at the moment
Solution
kekitek
kekitek3w 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
More Posts