Level 51
Check if it is null before parsing with Carbon. More info here:
https://stackoverflow.com/questions/45118001/carbon-get-current-date-if-variable-is-null
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In my Laravel-5.8 application, I have a field called hire_date. In the database, the datatype is date and default is NULL.
Model
protected $fillable = [
'id',
'employee_code',
'hire_date',
];
protected $dates = [
'hire_date',
];
public function setHireDateAttribute($value)
{
$this->attributes['hire_date'] = Carbon::createFromFormat('d-m-Y', $value);
}
public function getHireDateAttribute($input)
{
return Carbon::parse($input)->format('d-m-Y');
}
View
<p for="">: {{ $employee->hire_date }} </p>
I want to display None when the date is Null.
But why is it showing today's date (Hire Date 30-07-2020) instead of null?
Thanks
Check if it is null before parsing with Carbon. More info here:
https://stackoverflow.com/questions/45118001/carbon-get-current-date-if-variable-is-null
Please or to participate in this conversation.