Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

date field must cast to 'date' in eloquent model with a mysql date field #1114

Closed
Gil-1 opened this issue Dec 10, 2018 · 6 comments
Closed

Comments

@Gil-1
Copy link

Gil-1 commented Dec 10, 2018

  • Laravel Version: 5.7.15
  • Nova Version: 1.2.0
  • PHP Version: 7.2.12

Description:

When wanting to display a date (not a datetime) field I have the following error

date field must cast to 'date' in eloquent model

I already posted in laracasts nova's channel but there is no solution right now
https://laracasts.com/discuss/channels/nova/date-field-must-cast-to-date-in-eloquent-model-and-it-is

Steps To Reproduce:

I cast the date fields as this in the eloquent model :

protected $dates = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s',
        'deleted_at' => 'datetime:Y-m-d H:i:s',
        'billed_at' => 'date:Y-m-d',
        'payed_at' => 'date:Y-m-d',
        'due_date' => 'date:Y-m-d',
    ];

I also tested

protected $casts = [,
        'billed_at' => 'date',
        'payed_at' => 'date',
        'due_date' => 'date',
    ];
  • created_at, updated_at and deleted_at are stored as TIMESTAMP (no issue)
  • billed_at, payed_at and due_date are stored as DATE in Mysql

In my Nova Resource, if I use the Date field I have the error

date field must cast to 'date' in eloquent model

And this is how the fields are set

Date::make('Billed At', 'billed_at'),
Date::make('Payed At', 'payed_at'),
Date::make('Due Date', 'due_date'),
@dkulyk
Copy link

dkulyk commented Dec 10, 2018

In the dates field must be specified only property names:

protected $dates = [
    'billed_at', 'payed_at', 'due_date'
]

But casts must be work.

@Gil-1
Copy link
Author

Gil-1 commented Dec 10, 2018

I just tried and that doesn't work.

Here is what I tried

protected $casts = [
        'amount' => 'float(10,2)',
        'reference' => 'integer',
        'billed_at' => 'date',
        'payed_at' => 'date',
        'due_date' => 'date',
    ];
    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at',
    ];

@bonzai
Copy link

bonzai commented Dec 10, 2018

I can't confirm.

Migration

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->date('premium_expired_at')->nullable();
    $table->timestamps();
});

Model

class User extends Model
{
    protected $fillable = ['premium_expired_at'];
    protected $casts = ['premium_expired_at' => 'date'];
}

Resource

class User extends Resource
{
    public static $model = \App\User::class;

    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Date::make('Subscription', 'premium_expired_at')->nullable(),
        ];
    }
}

@dkulyk
Copy link

dkulyk commented Dec 10, 2018

Maybe you used mutators for this fields. Attribute casting doesn't work with mutators.

@Gil-1
Copy link
Author

Gil-1 commented Dec 10, 2018

That was it. I used mutators and that messed everything.
Thank you !

@bonzai bonzai closed this as completed Dec 10, 2018
@bonzai bonzai added the invalid label Dec 10, 2018
@GingerNinjaNicko
Copy link

I came across this issue whilst working on some data importers.
I wanted to default the value of published_at to the value of created_at and I found that you can use mutators providing you return a Carbon instance like so:

public function getPublishedAtAttribute($value)
{
    return \Carbon\Carbon::create($value ?? $this->attributes['created_at']);
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants