codewiser / laravel-enum-casting
This package is abandoned and no longer maintained.
No replacement package was suggested.
Casts set or array of enums
1.0.0
2022-06-06 11:19 UTC
Requires
- php: ^8.1
- laravel/framework: ^9.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
This package brings enums casting into Laravel projects.
Install
composer require codewiser/laravel-enum-casting
Database
We suppose that enums stored in database either as set
or as json
object.
Set
superuser,adminstrator
To cast enums from set
datatype, use set
keyword.
JSON
["superuser","adminstrator"]
To cast enums from json
datatype, use json
or array
keyword.
Usage
See also Laravel documentation
AsArray
Use AsArray
to cast enums as a simple array
. Provide datatype and Enum class as arguments:
use \Codewiser\Enum\Castable\AsArray; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'roles' => AsArray::class . ':set,' . MyEnum::class, ];
AsArrayObject
Use AsArrayObject
to cast enums as ArrayObject
. Provide datatype and Enum class as arguments:
use \Codewiser\Enum\Castable\AsArrayObject; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'roles' => AsArrayObject::class . ':json,' . MyEnum::class, ];
AsCollection
Use AsCollection
to cast enums as Collection
. Provide datatype and Enum class as arguments. Optionally you may pass custom collection class name:
use \Codewiser\Enum\Castable\AsCollection; /** * The attributes that should be cast. * * @var array */ protected $casts = [ 'roles' => AsCollection::class . ':array,' . MyEnum::class . ',' . MyCollection::class, ];
Arguments order has no matter.