Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Enumhancer Laravel Package

henzeb/enumhancer

View on GitHub
Deep Wiki
Context7

Eloquent Attribute Casting

Laravel supports casting backed enums out of the box, but what if you don't want to use backed enums? This is where CastsBasicEnumerations comes in.

Note: for attribute casting with State see here.

Usage

enum yourEnum {
    case MY_ENUM;
}

use Illuminate\Database\Eloquent\Model;
use Henzeb\Enumhancer\Laravel\Concerns\CastsBasicEnumerations;

class YourModel extends Model
{
    use CastsBasicEnumerations;

    $casts = [
        'column' => YourEnum::class
    ];
}

Lowercase values

By default, it will use the name of the basic enumeration as the value. If you want the lowercase variant, you can add the $keepEnumCase property and set it to false.

class YourModel extends Model
{
    use CastsBasicEnumerations;

    private $keepEnumCase = false;

    $casts = [
        'column' => YourEnum::class
    ];
}

State

When using State, You want to prevent state changes that aren't possible. CastsStatefulEnumerations does that. It will throw an IllegalEnumTransitionException when a transition is not allowed

enum elevator {
    use State;

    case Open;
    case Close;
    case Move;
    case Stop;
}

use Illuminate\Database\Eloquent\Model;
use Henzeb\Enumhancer\Laravel\Concerns\CastsStatefulEnumerations;

class YourModel extends Model
{
    use CastsStatefulEnumerations;

    $casts = [
        'state' => elevator::class
    ];

    /** Use this when you want to use hooks. */
    public function getTransactionHooks(string $attribute) : ?TransitionHooks{
        return match($attribute) {
            'state' => new YourElevatorHook($this->currentFloor);
        }
    }
}

Examples

$model = new YourModel();
$model->state = elevator::Open; // works
$model->state = elevator::Close; // works
$model->state = elevator::Stop; // IllegalEnumTransitionException

When you cast enums in your model that do not use State, but use the aggegrate trait Enhancers, you can either use the individual needed traits on that enum, or you can use the $castsIgnoreEnumState parameter on your Model.

class YourModel extends Model
{
    // list of column name(s) you wish to ignore
    protected $castsIgnoreEnumState = ['maintenance_mode'];
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
htinlynn/genius-helper
maco-studios/hephaestus
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator