1. Go to this page and download the library: Download azimkordpour/power-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.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
azimkordpour / power-enum example snippets
useAzimKordpour\PowerEnum\Traits\PowerEnum;
enum PostStatus: string
{
usePowerEnum;
case Active = 'active';
case Inactive = 'inactive';
}
namespaceApp\Models;
useApp\Enums\PostStatus;
useIlluminate\Database\Eloquent\Model;
classPostextendsModel{
/**
* The attributes that should be cast.
*
* @var array
*/protected $casts = [
'status' => PostStatus::class,
];
}
$post = Post::find(1);
// The status is active.
$post->status->isActive();
true
$post = Post::find(1);
// The status is active.
$post->status->equals(PostStatus::Active);
false
$post = Post::find(1);
// The status is active.
$post->status->is(PostStatus::Active);
false
$post = Post::find(1);
// The status is active.
$post->status->label();
/**
* Set the labels of all the cases.
*/publicstaticfunctionsetLabels(): array{
return [
self::Active->value => 'published post',
self::Inactive->value => 'draft post',
];
}