sfneal / casts
An alternative implementation of the Eloquent Model accessors & mutators pattern for type casting attributes
Installs: 54 308
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.0|^8.1|^8.2|^8.3
- ext-json: *
- nesbot/carbon: >=1.0
- vkovic/laravel-custom-casts: >=1.3
Requires (Dev)
- orchestra/testbench: ^7.40|^8.0|9.0
- phpunit/phpunit: ^9.6|^10.0|^11.0
- scrutinizer/ocular: ^1.8
- sfneal/mock-models: >=0.6
README
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 stephen.neal14@gmail.com 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.