feat: add Date factory support for model date handling #340
+449
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for the
Date::use()configuration in Eloquent models, allowing applications to useCarbonImmutable(or other custom date classes) throughout the framework. This matches Laravel's functionality.Hyperf's model date handling hardcodes
Carbon::directly, which means that even if you configureDate::use(CarbonImmutable::class)in a service provider, model date operations still return mutableCarboninstances. This PR fixes that by overriding the relevant methods to use theDatefacade instead.Problem
In Laravel, you can do this in a service provider:
And then the framework returns
CarbonImmutablefrom:'date','datetime','timestamp')created_at,updated_at)$model->freshTimestamp()Hypervel didn't support this because Hyperf hardcodes
Carbon::inHasAttributes::asDateTime()andHasTimestamps::freshTimestamp().Solution
Override the relevant methods in Hypervel to use the
Datefacade:HasAttributes- AddasDate()andasDateTime()methods that useDate::instance(),Date::parse(),Date::createFromTimestamp(), etc.HasTimestamps- New trait withfreshTimestamp()usingDate::now()Model,Pivot,MorphPivot- Use the newHasTimestampstraitChanges
HasAttributes.phpasDate()andasDateTime()methodsHasTimestamps.phpfreshTimestamp()Model.phpuse HasTimestampsPivot.phpuse HasTimestampsMorphPivot.phpuse HasTimestampsDateFactoryTest.phpTest Coverage
The test suite covers:
Date::now(),Date::parse(), etc.)Model::freshTimestamp()respects Date factoryasDateTime()handles all input types (Carbon, DateTime, timestamps, strings)DateFactory::useDefault()Usage