PHP code example of lemax10 / laravel-enums

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

    

lemax10 / laravel-enums example snippets


use App\Enums\ExampleStatusEnum;
use Illuminate\Database\Eloquent\Model;
use LeMaX10\Enums\Traits\ModelEnums;

/**
 * Class ExampleModel
 * @package App
 */
class ExampleModel extends Model
{
    use ModelEnums;

    /**
     * @var array
     */
    protected $enums = [
        'status' => ExampleStatusEnum::class
    ];
}

use LeMaX10\Enums\Enum;

/**
 * Class ExampleStatusEnum
 * @package App\Enums
 */
final class ExampleStatusEnum extends Enum
{
    /**
     *
     */
    private const FOO = 'foo';

    /**
     *
     */
    private const BAR = 'bar';
}

use LeMaX10\Enums\Enum;
use LeMaX10\Enums\Contracts\Translatable;

/**
 * Class ExampleStatusEnum
 * @package App\Enums
 */
final class ExampleStatusEnum extends Enum implements Translatable
{
    /**
     *
     */
    private const FOO = 'foo';

    /**
     *
     */
    private const BAR = 'bar';
    
    /**
     * Translatable value
     * @return string
     */
    public function getTransValue(): string
    {
        return trans('enum.'. $this->value);
    }
}




namespace App\Http\Requests;


use App\Enums\ExampleStatusEnum;
use Illuminate\Http\Request;

class ExampleRequest extends Request
{
    public function rules(): array
    {
        return [
            'status' => '




namespace App\Http\Requests;


use App\Enums\ExampleStatusEnum;
use LeMaX10\Enums\Rules\EnumValue;
use Illuminate\Http\Request;

class ExampleRequest extends Request
{
    public function rules(): array
    {
        return [
            'status' => ['




namespace App\Http\Requests;


use App\Enums\ExampleStatusEnum;
use Illuminate\Http\Request;

class ExampleRequest extends Request
{
    public function rules(): array
    {
        return [
            'status' => ['

'providers' => [
    ...
    \LeMaX10\Enums\EnumServiceProvider::class,
    ...
]

Schema::create('tableName', function (Blueprint $table): void {
            $table->increments('id');
            $table->enum('status', array_values(ExampleStatusEnum::toArray()));
        });
 php
$exampleModel = new ExampleModel;
$exampleModel->status = ExampleStatusEnum::FOO();
$exampleModel->save();
....


echo $exampleModel->status