Home > Software design >  Json column - Date field must cast to 'date' in Eloquent model
Json column - Date field must cast to 'date' in Eloquent model

Time:05-11

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

CodePudding user response:

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

  • Related