1. Go to this page and download the library: Download cerbero/laravel-enum library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cerbero / laravel-enum example snippets
use Cerbero\LaravelEnum\Concerns\Enumerates;
enum Numbers: int
{
use Enumerates;
case One = 1;
case Two = 2;
case Three = 3;
}
use Cerbero\Enum\Attributes\Meta;
enum PayoutStatuses
{
use Enumerates;
#[Meta(handler: SentPayoutHandler::class)]
case Sent;
#[Meta(handler: OnHoldPayoutHandler::class)]
case OnHold;
#[Meta(handler: DeclinedPayoutHandler::class)]
case Declined;
}
// 🐢 instead of this
$handler = match ($payout->status) {
PayoutStatuses::Sent => SentPayoutHandler::class,
PayoutStatuses::OnHold => OnHoldPayoutHandler::class,
PayoutStatuses::Declined => DeclinedPayoutHandler::class,
};
return Container::getInstance()->make($handler);
// 🐇 we can do this
return $payout->status->handler();
use Cerbero\Enum\Attributes\Meta;
#[Meta(handler: DefaultPayoutHandler::class)]
enum PayoutStatuses
{
use Enumerates;
#[Meta(handler: SentPayoutHandler::class)]
case Sent;
case OnHold;
case Declined;
}
use Cerbero\Enum\Attributes\Meta;
enum PayoutStatuses
{
use Enumerates;
#[Meta(handlePayout: SentPayoutHandler::class)]
case Sent;
#[Meta(handlePayout: OnHoldPayoutHandler::class)]
case OnHold;
#[Meta(handlePayout: DeclinedPayoutHandler::class)]
case Declined;
}
// 🐢 instead of this
$handler = match ($payout->status) {
PayoutStatuses::Sent => SentPayoutHandler::class,
PayoutStatuses::OnHold => OnHoldPayoutHandler::class,
PayoutStatuses::Declined => DeclinedPayoutHandler::class,
};
$handlePayout = Container::getInstance()->make($handler);
return $handlePayout($payout);
// 🐇 we can do this
return $payout->status->handlePayout($payout);
use Cerbero\Enum\Attributes\Meta;
#[Meta(handlePayout: DefaultPayoutHandler::class)]
enum PayoutStatuses
{
use Enumerates;
#[Meta(handlePayout: SentPayoutHandler::class)]
case Sent;
case OnHold;
case Declined;
}
use Cerbero\LaravelEnum\Contracts\Bitwise;
enum Permissions: int implements Bitwise
{
use Enumerates;
case CreatePost = 1;
case UpdatePost = 2;
case DeletePost = 4;
}
use Cerbero\LaravelEnum\CasesCollection;
class User extends Model
{
// before Laravel 11
protected $casts = [
'permissions' => CasesCollection::class . ':' . Permissions::class,
];
// after Laravel 11
protected function casts(): array
{
return [
'permissions' => CasesCollection::of(Permissions::class),
];
}
}
return [
Numbers::class => [
'One' => [
'description' => 'This is the case :value.',
],
],
];
// This is the case 1.
Numbers::One->description(value: 1);
$options = Numbers::pluck('description', 'value');
/*
[
1 => 'This is the case One.',
2 => 'This is the case Two.',
3 => 'This is the case Three.',
]
*/
use Cerbero\LaravelEnum\Concerns\EnumeratesCacheKeys;
enum CacheKeys: string
{
use EnumeratesCacheKeys;
case PostComments = 'posts.{int $postId}.comments';
case Tags = 'tags';
case TeamMemberPosts = 'teams.{string $teamId}.users.{string $userId}.posts';
}
use Cerbero\LaravelEnum\Concerns\EnumeratesSessionKeys;
enum SessionKeys
{
use EnumeratesSessionKeys;
case CartItems = 'cart-items';
case OnboardingStep = 'onboarding-step';
case FormsData = 'forms.{int $formId}.data';
}
use Cerbero\LaravelEnum\Enums;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
Enums::setPaths('app/Enums', 'domain/*/Enums');
}
}
use Cerbero\LaravelEnum\Enums;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
// custom static path
Enums::setTypeScript('frontend/enums/index.ts');
// custom dynamic path
Enums::setTypeScript(function (string $enum) {
$domain = explode('\\', $enum)[1];
return "resources/js/modules/{$domain}/enums.ts";
});
}
}