Looking to hire Laravel developers? Try LaraJobs

laravel-enum-casting maintained by codewiser

Description
Casts set or array of enums
Last update
2022/06/07 12:10 (dev-main)
License
Links
Downloads
15

Comments
comments powered by Disqus

Laravel Enum Casting

PHP Composer

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.