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.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
azimkordpour / power-enum example snippets
use AzimKordpour\PowerEnum\Traits\PowerEnum;
enum PostStatus: string
{
use PowerEnum;
case Active = 'active';
case Inactive = 'inactive';
}
namespace App\Models;
use App\Enums\PostStatus;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* 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('inactive');
false
$post = Post::find(1);
// The status is active.
$post->status->is('inactive');
false
$post = Post::find(1);
// The status is active.
$post->status->label();
/**
* Set the labels of all the cases.
*/
public static function setLabels(): array
{
return [
self::Active->value => 'published post',
self::Inactive->value => 'draft post',
];
}