healthengine/laravel-chronos

Swap out Carbon for Chronos

v4.0.0 2023-09-13 07:29 UTC

This package is auto-updated.

Last update: 2024-04-13 08:53:08 UTC


README

Latest Stable Version Total Downloads CircleCI

This is a custom package designed for Laravel Eloquent. It provides a trait that can be used in model classes that will cast datetime fields to ChronosInterface instead of Carbon instances.

Chronos is a drop-in replacement for Carbon but is immutable by default. It also provides an interface that can be used for some genericising as well.

See the Chronos documentation for more information.

Usage

Warning

This package is not compatible with Laravel's attribute casts: immutable_date, immutable_datetime.

To convert datetime casts from Carbon instances to Chronos instances in an Eloquent model, all you need to do is use this trait in the model. An example is shown below:

use HealthEngine\LaravelChronos\ChronosTimestamps;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use ChronosTimestamps;

    protected $casts = [
        // Throws error: "Call to undefined method Cake\Chronos\Chronos::toImmutable()"
        // 'processed_at' => 'immutable_date',
        // Throws error: "Call to undefined method Cake\Chronos\Chronos::toImmutable()"
        // 'processed_at' => 'immutable_datetime',
        'processed_at' => 'datetime',
    ];
}

License

Laravel Chronos is licensed under the MIT license.