An alternative implementation of the Eloquent Model accessors & mutators pattern for type casting attributes
MIT License
An alternative implementation of the Eloquent Model accessors & mutators pattern for type casting attributes
You can install the package via composer:
composer require sfneal/casts
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,
];
}
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the PHP Package Boilerplate.