Skip to content

sakanjo/laravel-easy-enum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”₯ Easy enum

Workflow status Laravel v11.x PHP 8.2

Easily work with enums.

✨ Help support the maintenance of this package by sponsoring me.

Designed to work with Laravel, Filament, and more.

Preview

Table of Contents

πŸ“¦ Install

composer require sakanjo/laravel-easy-enum

πŸ¦„ Usage

1. Create enum

<?php

namespace App\Enums;

use SaKanjo\EasyEnum;

enum ExampleEnum: int
{
    use EasyEnum;

    case Active = 0;
    case NOPE = 1;
}

2. Create lang file

// lang/en/enums.php

<?php

use App\Enums;

return [
    Enums\ExampleEnum::class => [
        Enums\ExampleEnum::NOPE->name => 'Nope',
        // ...
    ],
    
    // ...
];

That's it!

πŸ“š Methods

getLabel

Returns the label of the enum value.

Status::Active->getLabel(); // Active

is

Checks if the enum is equal to another one.

$enum1->is($enum2); // boolean

isNot

inverse of is.

$enum1->isNot($enum2); // boolean

in

Checks if the enum is in a list of enums.

$enum->in([$enum1, $enum2]); // boolean

notIn

inverse of in.

$enum->notIn([$enum1, $enum2]); // boolean

tryFromName

Safely converts a string to its corresponding enum value (returns null if not found).

Status::tryFromName('Active'); // Status::Active
Status::tryFromName('Oops'); // null

Converts a string to its corresponding enum value (throws exception if not found).

fromName

Status::fromName('Active'); // Status::Active
Status::fromName('Oops'); // Throws ValueError exception

names

Returns a list of case names.

Status::names(); // ['Active', 'NOPE']

values

Returns a list of case values .

Status::values(); // [0, 1]

options

Returns an associative array of case names and values.

Status::options(); // ['Active' => 0, 'NOPE' => 1]
Status::options(true); // ['Active' => 0, 'Nope' => 1]

toHtml

alias for getLabel, useful in blade.

Status::Active->toHtml(); // Active

resolveDisplayableValue

same as toHtml except it doesn't render HTML.

Status::Active->resolveDisplayableValue(); // Active

πŸ”₯ Practical examples

Filamentphp

Enum
<?php

namespace App\Enums;

use Filament\Support\Contracts\HasLabel;
use SaKanjo\EasyEnum;

enum Status: int implements HasLabel
{
    use EasyEnum;

    case Active = 0;
    case Disabled = 1;
}
Resource
<?php

use Filament\Forms;
use App\Enums;

Forms\Components\Select::make('status')
    ->options(Enums\Status::class);

Laravel blade

<?php

namespace App\Enums;

use Illuminate\Contracts\Support\DeferringDisplayableValue;
use Illuminate\Contracts\Support\Htmlable;
use SaKanjo\EasyEnum;

enum Status: int implements Htmlable // or DeferringDisplayableValue
{
    use EasyEnum;

    case Active = 0;
    case Disabled = 1;
}
<div>
    Current status: {{ auth()->user()->status }}
</div>

πŸ’– Support the development

Do you like this project? Support it by donating

Click the "πŸ’– Sponsor" at the top of this repo.

©️ Credits

πŸ“„ License

MIT License Β© 2023-PRESENT Salah Kanjo