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.
- Host: GitHub
- URL: https://github.com/atomjoy/laravel-enum
- Owner: atomjoy
- Created: 2025-04-30T09:50:36.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-30T10:20:45.000Z (about 1 year ago)
- Last Synced: 2025-05-09T01:59:34.365Z (about 1 year ago)
- Topics: laravel-enum
- Language: PHP
- Homepage: https://github.com/atomjoy/laravel-enum/tree/main
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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
];
```