-
-
Notifications
You must be signed in to change notification settings - Fork 394
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
Eloquent model timestamps are treated as Carbon\Carbon|null #1580
Comments
Hellooo @magnetas! 👋🏻 includes:
- vendor/nesbot/carbon/extension.neon |
...as for this specific case: try returning a non-nullable value. |
Hi @szepeviktor! First of all thank you for such a prompt reaction! I wasn't aware of this, but unfortunately it doesn't help with my case - appended it to the includes list and keep getting the same errors. |
well...the thing is eloquent model timestamp fields are nullable (i.e. before persisting them they are null. Also DB columns behind them are nullable). Tried hinting my return as ?Carbon (nullable support one) - no luck. |
Yes, this seems to be related. If this assertion passes it'd mean that created_at is nullable Carbon\Carbon instance. However it isn't since Laravel has the wrapper (I guess mostly for mocking). |
@szepeviktor could this be the problem? If |
You can debug it in your function: |
Technically which passes as well... |
Neat! This is what I get:
|
@canvural Could you help? I'm lost. |
@magnetas is right here. The union of |
Just commented these three lines out in my example project, cleared phpstan cache and ran analysis again - all green. Also Not sure why the union was needed there in the first place. I think what this part of the cast helper does is it determines what type is returned when casting datetimes/timestamps and the union should not be needed. However it may be needed when defining writable type (since \Carbon\Carbon or even string can be used to set the value. Might be missing some context here too. If that's the case then maybe the property extension should do the checks and decide whether additional union is needed... |
Just tried applying the same (commenting out those three lines with union) to actual Laravel 10 project. I get 18 such errors across the codebase using phpstan level 3. Skipping the union meant no more errors. Now I could prepare a PR if this is REALLY how it should behave, but again - I might be missing something |
--level
used: 3Description
Larastan treats/resolves eloquent model timestamps being of
Carbon\Carbon|null
type instead ofIlluminate\Support\Carbon
.Prepared a simple case in a fresh Laravel project. Basically my arrow function says it'll return an
Illuminate\Support\Carbon
instance and that's the eloquent castedcreated_at
value. Get the following error:Anonymous function should return Illuminate\Support\Carbon but returns Carbon\Carbon|null
If I check the actual type (i.e. dd($user->created_at)) it in fact is
Illuminate\Support\Carbon
.Any clue how this can be sorted out?
Thank you!
Laravel code where the issue was found
The text was updated successfully, but these errors were encountered: