PHP code example of desmart / laravel-enum

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

    

desmart / laravel-enum example snippets


class Character extends DeSmart\Laravel\Enumeration
{
    const GOOD = 'good';
    const EVIL = 'evil';
    const SOMETIMES_GOOD_SOMETIMES_EVIL = 'sometimes_good_sometimes_evil';
}

class Hero extends Model
{
    /**
     * @var array
     */
    protected $casts = [
        'character' => Character::class,
    ];
}

$hero = new Hero(['character' => Character::EVIL]);

dump($hero);
// Hero {#293
//  ...
//  #casts: array:1 [
//    "character" => "Character"
//  ]
// ...
//  #attributes: array:1 [
//    "character" => "evil"
//  ]
// }

dump($hero->character);
// Character {#296
//  -value: "evil"
// }

php artisan make:enum Character --cases='good,evil,sometimes_good_sometimes_evil'

namespace App\Enums;

use DeSmart\Laravel\Enumeration\Enumeration;

/**
 * @method static Character good()
 * @method static Character evil()
 * @method static Character sometimesGoodSometimesEvil()
 */
class Character extends Enumeration
{
	const GOOD = 'good';
	const EVIL = 'evil';
	const SOMETIMES_GOOD_SOMETIMES_EVIL = 'sometimes_good_sometimes_evil';
}