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

Default date field casts are ignored #1744

Open
john-dent opened this issue Sep 23, 2023 · 3 comments · May be fixed by #1842
Open

Default date field casts are ignored #1744

john-dent opened this issue Sep 23, 2023 · 3 comments · May be fixed by #1842

Comments

@john-dent
Copy link

Readable / writable type for Laravels default date columns ignores any model casts.

Can be replicated by casting updated_at, created_at or deleted_at; you will notice larastan does not respect the cast.

Our specific use case is to return an array of formatted dates, larastan still expects the return type to be of Carbon|null.

I can create a PR for this issue that would first respect the cast and fallback to the default date format, if this is something that may be approved?

Many thanks,

@szepeviktor
Copy link
Collaborator

Hello @john-dent 👋🏻
Thank you for your report and offer.

Our specific use case is to return an array of formatted dates

Could you show us in code how you do that?

@john-dent
Copy link
Author

john-dent commented Sep 23, 2023

Could you show us in code how you do that?

Sure, something along the lines of the following in a custom cast:

    public function get($model, $key, $value, $attributes)
    {
        return [
            'raw' => $value ?? null,
            'formatted' => $value?->format('jS M Y G:i'),
            'ago' => $value?->diffForHumans(),
        ];
    }

@john-dent
Copy link
Author

john-dent commented Sep 23, 2023

@szepeviktor

Specific issue appears to be in /src/Properties/ModelPropertyExtension.php Lines 151-154, offhand I think a suitable fix would be as simple as swapping the if and elseif.

https://github.com/nunomaduro/larastan/blob/4ba82ba350e5a5288154454cc069bda6af8b7aba/src/Properties/ModelPropertyExtension.php#L151-L164

@calebdw calebdw linked a pull request Feb 11, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants