casts

An alternative implementation of the Eloquent Model accessors & mutators pattern for type casting attributes

MIT License

Downloads
53.6K
Stars
0
Committers
1

Casts

An alternative implementation of the Eloquent Model accessors & mutators pattern for type casting attributes

Installation

You can install the package via composer:

composer require sfneal/casts

Usage

In order to make use of the attribute type Casts add the HasCustomCasts trait to your model before definition attributes that should be cast to custom types.

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Sfneal\Casts\CarbonCast;
use Sfneal\Casts\NewlineCast;
use Sfneal\Casts\NullableIntArrayCast;
use Sfneal\LaravelCustomCasts\HasCustomCasts;

class People extends Model
{
    use HasCustomCasts;

    protected $table = 'people';
    protected $primaryKey = 'person_id';

    protected $fillable = [
        'person_id',
        'name_first',
        'name_last',
        'email',
        'birthday',
        'bio',
        'favorites',
    ];

    /**
     * @var array Attributes that should be type cast
     */
    protected $casts = [
        'birthday' => CarbonCast::class,
        'bio' => NewlineCast::class,
        'favorites' => NullableIntArrayCast::class,
    ];
}

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

PHP Package Boilerplate

This package was generated using the PHP Package Boilerplate.

Package Rankings
Top 20.15% on Packagist.org
Badges
Extracted from project README
Packagist PHP support Latest Version on Packagist Scrutinizer Code Quality Total Downloads