6

I am having a hard time understanding the inner workings of date fields in laravel. I have a custom date field named called_at and I set it as nullable in the schema. Since it's a date field, I would naturally want it to be a Carbon instance, so in the model I pushed this attribute to the $dates array.

protected $dates = ['called_at'];

In order to be able to set it from the controller, via the request('called_at') input, I would have to set a mutator in the model. So I set one like this:

public function setCalledAtAttribute($date){
    $this->attributes['called_at'] = Carbon::parse($date);
}

But the problem is, even if the user does not provide a date in the input field, the called_at attribute is set to the current timestamp! But I expected it to be NULL.

To get rid of it, I commented out the setCalledAtAttribute mutator. But now it won't even take the input:

InvalidArgumentException Data missing

Bottom line, how can I set my date field nullable in such a way that when a user leaves the input field blank, the called_at field wont be set to current timestamp?

4
  • 1
    Within your migration $table->timestamp('called_at) just add ->nullable() and then re migrate. Or go in to the database and simply allow null on the field. Commented Feb 22, 2018 at 10:47
  • I already set it as nullable() in the schema. Commented Feb 22, 2018 at 10:48
  • Did it register? Commented Feb 22, 2018 at 10:48
  • I mentioned this in the question: "I have a custom date field named called_at and I set it as nullable in the schema" Commented Feb 22, 2018 at 10:50

2 Answers 2

8

Try to do this:

public function setCalledAtAttribute($date)
{
    $this->attributes['called_at'] = empty($date) ? null : Carbon::parse($date);
}
Sign up to request clarification or add additional context in comments.

6 Comments

It's working. I have a question though. Why does it set the date field to current timestamp even if nothing is inserted in the form?
@Eisenheim define the date as ->dateTime() and not ->timestamp()
if I define it as dateTime(), will I be able to take empty value from the form?
Even if I leave it as timestamp and don't set it to dateTime, your code still works (sets null instead of current date).
@Eisenheim Note that timestamp fields will automatically update any time the record is created or updated. If you don't want this behaviour, then change it to a datetime field.
|
2

If you use parse on an empty string Carbon::parse('') or null Carbon::parse(null) Carbon will actually return the current date and time So you should check if $date is provided and not null:

$this->attributes['called_at'] = $date ? Carbon::parse($date); : null; 

1 Comment

thanks for making the issue clear. Laravel should have added this in their documentation. Not everyone has prior knowledge on Carbon :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.