PHP code example of sakanjo / laravel-easy-enum

1. Go to this page and download the library: Download sakanjo/laravel-easy-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/ */

    

sakanjo / laravel-easy-enum example snippets




namespace App\Enums;

use SaKanjo\EasyEnum;

enum ExampleEnum: int
{
    use EasyEnum;

    case Active = 0;
    case NOPE = 1;
}

// lang/en/enums.php



use App\Enums;

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

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

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

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

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

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

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

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

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

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

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

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

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



namespace App\Enums;

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

enum Status: int implements HasLabel
{
    use EasyEnum;

    case Active = 0;
    case Disabled = 1;
}



use Filament\Forms;
use App\Enums;

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



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>