Easily work with enums.
โจ Help support the maintenance of this package by sponsoring me.
Designed to work with Laravel, Filament, and more.
composer require sakanjo/laravel-easy-enum
<?php
namespace App\Enums;
use SaKanjo\EasyEnum;
enum ExampleEnum: int
{
use EasyEnum;
case Active = 0;
case NOPE = 1;
}
// lang/en/enums.php
<?php
use App\Enums;
return [
Enums\ExampleEnum::class => [
Enums\ExampleEnum::NOPE->name => 'Nope',
// ...
],
// ...
];
That's it!
Returns the label of the enum value.
Status::Active->getLabel(); // Active
Returns the translated label of the enum value.
Status::Active->translated('tr'); // Aktif
Checks if the enum is equal to another one.
$enum1->is($enum2); // boolean
inverse of is
.
$enum1->isNot($enum2); // boolean
Checks if the enum is in a list of enums.
$enum->in([$enum1, $enum2]); // boolean
inverse of in
.
$enum->notIn([$enum1, $enum2]); // boolean
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).
Status::fromName('Active'); // Status::Active
Status::fromName('Oops'); // Throws ValueError exception
Returns a list of case names.
Status::names(); // ['Active', 'NOPE']
Returns a list of case values .
Status::values(); // [0, 1]
Returns an associative array of case names and values.
Status::options(); // ['Active' => 0, 'NOPE' => 1]
Status::options(true); // ['Active' => 0, 'Nope' => 1]
alias for getLabel
, useful in blade.
Status::Active->toHtml(); // Active
same as toHtml
except it doesn't render HTML.
Status::Active->resolveDisplayableValue(); // Active
<?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;
}
<?php
use Filament\Forms;
use App\Enums;
Forms\Components\Select::make('status')
->options(Enums\Status::class);
<?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>
Do you like this project? Support it by donating
Click the "๐ Sponsor" at the top of this repo.
MIT License ยฉ 2023-PRESENT Salah Kanjo
laravel-easy-enum's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.