Laravel uses the $dates variable
class User extends Model {
/**
* Convert date fields to Carbon
*
* @var array
*/
protected $dates = ['birthdate'];
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi there.
If I add this in my model :
public function getDates()
{
return ['visibility_on', 'validity_from', 'validity_to'];
}
I have an InvalidArgumentException error with data missing when I try to add a new article. Now, if I remove it, all is alright.
What's going wrong ?
Laravel uses the $dates variable
class User extends Model {
/**
* Convert date fields to Carbon
*
* @var array
*/
protected $dates = ['birthdate'];
}
protected $dates = ['visibility_on', 'validity_from', 'validity_to', 'deleted_at'];
public function getDates()
{
return ['visibility_on', 'validity_from', 'validity_to'];
}
Still the same error
You have overridden the Eloquent's getDates() method. You shouldn't do that.
Instead try @blackbird's approach.
If you really want to override it and use getDates() method for this, then you can do it like this:
public function getDates()
{
return array_merge(parent::getDates(), ['visibility_on', 'validity_from', 'validity_to']);
}
but I don't recommend you do it that way. Just ommit the getDates() method you've created and use $dates property to set which fields should be Carbon instances, like so
class User extends Model {
protected $dates = ['visibility_on', 'validity_from', 'validity_to'];
}
Remove the getDates function, you don't need it if you use the $dates variable ;)
Okay just removed it but now when I put all fields in the $dates, I have the same error. But if I remove all and just put
protected $dates = ['deleted_at'];
instead
protected $dates = ['visibility_on', 'validity_from', 'validity_to', 'deleted_at'];
That's working. Fields are timestamp format
are you sure the fields have the proper format in mysql/postgres?
Yes. created_at, updated_at, deleted_at, visibility_on, visibility_from, visibility_to are in the same timestamp format.
So I have this as an example for you ;)
// Migration
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->date('birthdate');
$table->text('bio');
$table->timestamps();
});
// Model
class User extends Model {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* Convert date fields to Carbon
*
* @var array
*/
protected $dates = ['birthdate'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'birthdate', 'bio'];
}
@blackbird Thanks for your example. I understand it but to come back to my code, If for example, I put 'visibity_from' in the $dates array and I fill the field, that's working. But if I don't fill it, that don't. Obviously, I put this field to nullable. I don't understand why it don't working in that way.
Your model and migrations are fine then. I think you have something else wrong! You said that you earlier had an InvalidArgumentException, on what line was this and what is the code that belongs with that?
The error page :
InvalidArgumentException in Carbon.php line 385:
Data missing
in Carbon.php line 385
at Carbon::createFromFormat('Y-m-d H:i:s', '') in Model.php line 2885
at Model->asDateTime('') in Model.php line 2390
at Model->attributesToArray() in Model.php line 2369
at Model->toArray() in EloquentBaseModel.php line 16
at EloquentBaseModel::CeProtelco\{closure}(object(Advantage))
at call_user_func_array(object(Closure), array(object(Advantage))) in Dispatcher.php line 218
at Dispatcher->fire('eloquent.saving: CeProtelco\Advantage', object(Advantage), true) in Dispatcher.php line 165
at Dispatcher->until('eloquent.saving: CeProtelco\Advantage', object(Advantage)) in Model.php line 1676
at Model->fireModelEvent('saving') in Model.php line 1482
at Model->save() in Model.php line 544
at Model::create(array('_token' => 'h9vuO0zwRsHDEC5zL9FeTbi2i5aN29UuX1UUNULZ', 'advantage_category_id' => '2', 'name' => 'Test', 'slug' => 'test', 'desc' => '', 'phone' => '', 'email' => '', 'website' => '', 'city_id' => '', 'validity_from' => '', 'validity_to' => '', 'event' => '', 'visibility' => '1', 'visibility_on' => '', 'contain' => 'product')) in AdvantagesController.php line 76
My controller :
$advantage = $this->advantage->create($request->all());
So the date field is empty!
// The empty string in the function should be the data that is posted in the form
at Carbon::createFromFormat('Y-m-d H:i:s', '') in Model.php line 2885
Do you have a date input field setup? Are you sure the correct data is posted?
Did you try something like this to see if you get the correct data?
dd($request->all()); // die and dump the data to the screen
$advantage = $this->advantage->create($request->all());
array:15 [▼
"_token" => "h9vuO0zwRsHDEC5zL9FeTbi2i5aN29UuX1UUNULZ"
"advantage_category_id" => "1"
"name" => "test"
"slug" => "test"
"desc" => ""
"phone" => ""
"email" => ""
"website" => ""
"city_id" => ""
"validity_from" => ""
"validity_to" => ""
"event" => ""
"visibility" => "1"
"visibility_on" => ""
"contain" => "product"
]
The field is empty because the date is not obligatory ;)
You need to put a date in there otherwise Carbon can't create a date for it!
Yeah I see it but is there any way to escape this ? Perhaps with a mutator ?
You mean you want to set the date standard?
You can do something like this in your migration:
$table->timestamp('validity_from')->default(Carbon::now();
Or:
$table->date('validity_from')->default(Carbon::now());
To test it, I try :
public function setValidityFromAttribute($value)
{
$this->attributes['validity_from'] = $value ? Carbon::createFromFormat('Y-m-d', $value) : NULL;
}
That's working. Because I need to set a NULL value if that's empty
Aah ok, that was not clear to me...
No problem, you helped me to find the solution :) In fact, that should be an improvement to do with dates in Laravel. A way to tell, be caution, this date can be empty.
No problem :)
@fdusautoir Just filter your input and don't pass empty strings. Imagine nullable varchar field - it would do just the same: instead of null you would have '' value in the table, which is rather not what you expect. dates is different because of the Carbon mutation.
Please or to participate in this conversation.