healthengine/laravel-chronos

This package is abandoned and no longer maintained. The author suggests using the laravel/framework package instead.

Swap out Carbon for Chronos

v4.0.1 2024-06-28 02:00 UTC

This package is auto-updated.

Last update: 2024-08-28 00:08:15 UTC


README

Warning

This repository is archived. Use immutable date casts in laravel/framework instead.

Laravel Chronos

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.