1. Go to this page and download the library: Download jundayw/laravel-enumeration 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/ */
jundayw / laravel-enumeration example snippets
namespace App\Enums;
use Jundayw\LaravelEnumeration\Annotation\Attributes;
use Jundayw\LaravelEnumeration\Concerns\HasEnumeration;
use Jundayw\LaravelEnumeration\Contracts\Enumeration;
enum Test: string implements Enumeration
{
use HasEnumeration;
#[Attributes(attribute: '正常')]
case NORMAL = 'NORMAL';
#[Attributes(attribute: '禁用')]
case DISABLE = 'DISABLE';
#[Attributes(attribute: 'Attribute')]
case NAME = 'VALUE';
}
namespace App\Models;
use App\Enums\Test;
use Illuminate\Database\Eloquent\Model;
class Manager extends Model
{
/**
* @var string[]
*/
protected $casts = [
'state' => Test::class,
];
}
use App\Enums\Test;
dd(Test::cases());
dd(Test::values());
use App\Enums\Test;
use App\Models\Manager;
$manager = new Manager();
$manager->state = Test::NAME;
$manager->state = Test::from('VALUE');
$manager->state = Test::valueOf('value')->getDeclaringClass();
$manager->state = Test::valueOf('VALUE')->getDeclaringClass();
$manager->state = Test::valueOf('VALUE')->getDeclaringClass()->value;
use App\Enums\Test;
use App\Models\Manager;
$manager = (new Manager())->find(1);;
dd($manager->state);