Hi I'm using laravel nova for create an admin panel. And I'm trying to use Date Field.
This is my migration,
$table->date('day')->nullable();
This is my my nova resource,
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Date::make(__('Day'), 'day'),
];
}
This is my model,
class Menu extends Model
{
use HasTranslations;
public $translatable = ['name','description'];
protected $fillable = [
'name','description','typology', 'cost', 'day', 'active', 'buffet'
];
This is my error,
Date field must cast to 'date' in Eloquent model.
Do I need to do anything in resource ?
In your model class you just need to add the following lines which describe to laravel that you must cast the day field into a date object (Carbon):
//Casts of the model dates
protected $casts = [
'day' => 'date'
];
Check here someone has the same issue.
EDIT:
I have seen that your day
column is set to nullable
i think that your field Nova should be to like in this post :
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Date::make(__('Day'), 'day')->nullable(),
];
}
And we need to change the model like this,
protected $casts = ['day' => 'date'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With