1

I have a laravel nova panel like below. Im trying to use a Date field like below:

new Panel('Suggestions', [
    Flexible::make('Suggestions')
        ->addLayout('Suggestions', 'suggestion', [
            Text::make('Title'),
            Textarea::make('Message'),
            Date::make('Date', 'date')
        ])
        ->button('Add Suggestion'),
]),
        

However it shows this error:

{message: "Date field must cast to 'date' in Eloquent model.", exception: "Exception",…}
exception: "Exception"
file: "/var/www/html/vendor/laravel/nova/src/Fields/Date.php"
line: 41
message: "Date field must cast to 'date' in Eloquent model."

I have in the model the casts property like this:

protected $casts = [
    'date' => 'date'
];

Do you know what can be the issue?

I don't have a date field on the database table relative to the model. I just have a suggestions json field that has a lot of columns including the "date" column/key, probably that's the issue. Do you know how to solve this issue in this scenario? Thanks

0

2 Answers 2

2

Add this to your casts instead of 'date' => 'date', this solved for me the issue.

protected $casts = [
        'flexible-content' => FlexibleCast::class
    ];

Another options is resolving it and converting it to a MomentJS format which also works fine:

DateTime::make('Date')
   ->format('DD/MM/YYYY HH:mm')
   ->resolveUsing(function ($value) {
           return $value;
   }),

source: https://github.com/whitecube/nova-flexible-content/issues/171

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! Like that it doesn't show the error. Do you know how to change so it have this format "YYYY/MM/DD" and to show the current date by default? Like this is not working : Date::make('Date') ->format('YYYY/MM/DD') ->resolveUsing(function ($value) { return $value; }), ]).
As per documentation (nova.laravel.com/docs/1.0/resources/fields.html#datetime-field) you could just remove the format because it will default format it to that format, otherwise you could look at Moment.js formatting rules momentjs.com/docs/#/displaying/format because nova dates standard uses MomentJS for formatting dates
Thanks but without the format it shows with this format "dd/mm/yyy". And the field shows "dd/mm/yyyy" instead of the current date by default.
You should take a look then at the MomentJS formatting rules I gave you above, and otherwise you could still transform you date in the resolveUsing with carbon to the needed format. Maybe not the cleanest fix but that should also work.
Thanks, but can you explain how it would work in the resolveUsing()? To have the "yyyy/mm/dd" format?
|
0

First, I guess you need to rename your db field to something else.

And then

You need to cast your db field into your model like this:

//Casts of the model dates
protected $casts = [
    'DB_FIELD_NAME' => 'date'
];

Comments

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.