An open API service indexing awesome lists of open source software.

https://github.com/atomjoy/laravel-enum

Laravel enum examples.
https://github.com/atomjoy/laravel-enum

laravel-enum

Last synced: 9 months ago
JSON representation

Laravel enum examples.

Awesome Lists containing this project

README

          

# laravel-enum
Laravel enum examples.

## Roles

```php
'Admins',
static::WRITER => 'Writers',
static::SUPERADMIN => 'Super Admins',
default => throw new \Exception('Unknown enum value requested for the label.'),
};
}
}
```

## Use

```php
value;
$label = RolesEnum::ADMIN->label();

$roles = RolesEnum::cases();

foreach (RolesEnum::cases() as $item) {
Role::create(['name' => $item->value, 'guard_name' => 'web']);
}

// Invalid data throws error
$role = RolesEnum::from('admin');

var_dump($role); // enum(RolesEnum::ADMIN)
echo $role->name; // "ADMIN";
echo $role->value; // "admin"

// Invalid data return null
$role = RolesEnum::tryFrom('not-existing');

var_dump($role); // null
```

## Casts

```php
protected $casts = [
'user_role' => \App\Enums\RolesEnum::class
];
```