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

Error in default casts #957

Closed
joaopalopes24 opened this issue Feb 25, 2025 · 1 comment
Closed

Error in default casts #957

joaopalopes24 opened this issue Feb 25, 2025 · 1 comment

Comments

@joaopalopes24
Copy link

joaopalopes24 commented Feb 25, 2025

✏️ Describe the bug
If I create one property with Carbon type, and other primitive type, dispatch exception.

↪️ To Reproduce
Provide us a pest test like this one which shows the problem:

it('error in Carbon properties with primitive type', function () {
    class BaseData extends Data
    {
        public function __construct(
            public Carbon|int|null $date,
        ) {
        }
    }

    // Dispatch CannotCastDate Exception: Could not cast date `5` into a `Illuminate\Support\Carbon` using formats: Y-m-d\TH:i:sP
    dd($data = BaseData::from(['date' => 5]));

    expect($data->date)->toBe(5);
});

✅ Expected behavior
Accept the int value too.

🖥️ Versions

Laravel: 11.41.3
Laravel Data: 4.13.1
PHP: 8.4.3

@rubenvanassche
Copy link
Member

You'll have to modify how data is casted: https://spatie.be/docs/laravel-data/v4/advanced-usage/working-with-dates

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

2 participants