1

I'm using Laravel 5.1 on a Homestead Vagrant box, PHP 7.2.14 on Ubuntu 18.04 with MySql 5.7.25.

I have created a migration with this:

$table->date('expiry_date')->nullable()->default(null);

I have a form with a date field called expiry_date, with a default set to null:

{!! Form::date('expiry_date', null, array('class' => 'form-control')) !!}

When I enter a date in the field everything works fine, but when I leave it blank it inserts 0000-00-00 into the database, which then messes up the display in my view. The only way I've been able to fix it is by adding this to the model:

public function setExpiryDateAttribute($value)
{
    $this->attributes['expiry_date'] = $value ?: null;
}

I'm annoyed that I have to do this extra step, and it doesn't feel very "Laravel" which makes me believe I'm missing something.

Interesting thing is that it passes validation in my custom Request:

'expiry_date' => 'date',

EDIT:

I have protected $dates ['expiry_date'] set in the model and it's been added to mass assignment as well.

Andrew Fox
  • 794
  • 4
  • 13
  • 30
  • As far as I know you can't null the time. It's default is 0000-00-00 00:00. – Irfan Sep 05 '20 at 23:13
  • 1
    html5's `input type="date"` default might be `0000-00-00` if empty... so technically it's not null. see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date – O-9 Sep 05 '20 at 23:25
  • Thanks @O-9 I'll give that a try and get back to you – Andrew Fox Sep 09 '20 at 17:06

0 Answers0