Skip to content

Fixes: getting immutable_datetime property fails if Date::use(CarbonImmutable::class) is set #3342

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

Merged

Conversation

saineshmamgain
Copy link
Contributor

This PR fixes the issue #3341 .

Basically when Date::use(CarbonImmutable::class); is added. The method asDateTime in DocumentModel trait throws an exception. I've changed the return type of asDateTime method from Carbon to DateTimeInterface.

Checklist

  • Add tests and ensure they pass

…setting `Date::use(CarbonImmutable::class);`
…able-property' into fix/error-accessing-carbon-immutable-property
…able-property' into fix/error-accessing-carbon-immutable-property
…able-property' into fix/error-accessing-carbon-immutable-property
@saineshmamgain saineshmamgain requested a review from a team as a code owner April 9, 2025 15:29
@saineshmamgain saineshmamgain requested a review from jmikola April 9, 2025 15:29
Copy link
Member

@GromNaN GromNaN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a minor comment, but the overall seems good to me. Thanks!

* @mixin Eloquent
* @method static Builder create(...$values)
* @method static Builder truncate()
* @method static Eloquent sole(...$parameters)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These annotations are interesting, I think we should document this practice (something I will ask the doc team).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

credit goes to @Treggats. I followed his HiddenAnimal model.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to help :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GromNaN the thing with Laravel Models is that by default they have the type Model. Which makes sense, as the model extends that class, but then the IDE doesn't know about any of the methods, relations, properties, etc.

These docblocks provide the IDE with the context it should have. Note; the @method docblock can not be used if the method already exists in the class.

Personally I also use @property for the columns of the database table and the relation once fetched.
The @property-read docblock is used for the Builder return type of a relation.

A little gotcha is that sometimes you need a additional docblock on the variable. Like this.

use Tests\Models\Anniversary;

/** @var Anniversary $anniversary */
$anniversary = Anniversary::query()->where('name', 'John')->first();

It then knows about the properties, relations, etc.

Hope this helps.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'm tracking this to work on it later PHPORM-316

@GromNaN
Copy link
Member

GromNaN commented Apr 10, 2025

@saineshmamgain can you look at the test failure:

1) MongoDB\Laravel\Tests\Eloquent\DateTimeImmutableTest::testCanReturnCarbonImmutableObject
* Test code or tested code did not remove its own error handlers

* Test code or tested code did not remove its own exception handlers

/home/runner/work/laravel-mongodb/laravel-mongodb/tests/DateTimeImmutableTest.php:28

@saineshmamgain
Copy link
Contributor Author

@GromNaN actually this error comes when I do the reset Date::useDefault(); in tearDown Method.

@saineshmamgain
Copy link
Contributor Author

@GromNaN My bad missed call to parent::tearDown() in my method. Updated the PR.

@GromNaN GromNaN merged commit 8b5ab59 into mongodb:5.x Apr 10, 2025
70 checks passed
@GromNaN
Copy link
Member

GromNaN commented Apr 10, 2025

Thank you @saineshmamgain

@GromNaN GromNaN added the bug label Apr 10, 2025
@GromNaN GromNaN added this to the 5.3 milestone Apr 10, 2025
@saineshmamgain saineshmamgain deleted the fix/error-accessing-carbon-immutable-property branch April 11, 2025 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants