Skip to content

Conversation

@binaryfire
Copy link
Contributor

Summary

This PR adds support for the Date::use() configuration in Eloquent models, allowing applications to use CarbonImmutable (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 configure Date::use(CarbonImmutable::class) in a service provider, model date operations still return mutable Carbon instances. This PR fixes that by overriding the relevant methods to use the Date facade instead.

Problem

In Laravel, you can do this in a service provider:

use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Date;

public function boot()
{
    Date::use(CarbonImmutable::class);
}

And then the framework returns CarbonImmutable from:

  • Date casts ('date', 'datetime', 'timestamp')
  • Model timestamps (created_at, updated_at)
  • $model->freshTimestamp()

Hypervel didn't support this because Hyperf hardcodes Carbon:: in HasAttributes::asDateTime() and HasTimestamps::freshTimestamp().

Solution

Override the relevant methods in Hypervel to use the Date facade:

  • HasAttributes - Add asDate() and asDateTime() methods that use Date::instance(), Date::parse(), Date::createFromTimestamp(), etc.
  • HasTimestamps - New trait with freshTimestamp() using Date::now()
  • Model, Pivot, MorphPivot - Use the new HasTimestamps trait

Changes

File Change
HasAttributes.php Added asDate() and asDateTime() methods
HasTimestamps.php New trait with freshTimestamp()
Model.php Added use HasTimestamps
Pivot.php Added use HasTimestamps
MorphPivot.php Added use HasTimestamps
DateFactoryTest.php 22 tests covering all scenarios

Test Coverage

The test suite covers:

  • Date facade returns configured class (Date::now(), Date::parse(), etc.)
  • Model::freshTimestamp() respects Date factory
  • asDateTime() handles all input types (Carbon, DateTime, timestamps, strings)
  • Date and datetime casts return configured class
  • Pivot and MorphPivot models respect Date factory
  • Reset to default with DateFactory::useDefault()

Usage

// In a service provider
use Carbon\CarbonImmutable;
use Hypervel\Support\Facades\Date;

public function boot()
{
    Date::use(CarbonImmutable::class);
}

// Now all model dates are immutable
$user = User::find(1);
$user->created_at; // CarbonImmutable instance
$user->freshTimestamp(); // CarbonImmutable instance

Override Hyperf's date handling methods to use the Date facade instead of
hardcoding Carbon directly. This allows applications to configure a custom
date class (e.g., CarbonImmutable) via Date::use() and have it respected
throughout the framework.

Changes:
- Add asDate() and asDateTime() methods to HasAttributes trait
- Create HasTimestamps trait with freshTimestamp() using Date::now()
- Update Model, Pivot, and MorphPivot to use the new HasTimestamps trait
- Add comprehensive test coverage for DateFactory integration
@albertcht albertcht changed the title fix: add Date factory support for model date handling feat: add Date factory support for model date handling Jan 8, 2026
@albertcht albertcht added the enhancement Improved feature or adjustments. label Jan 8, 2026
@albertcht albertcht merged commit 4094802 into hypervel:main Jan 8, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Improved feature or adjustments.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants