Laravel Packages

Laravel Enum Package for Generating enum-php Classes

Published Updated
Laravel Enum Package for Generating enum-php Classes image

Laravel Enum by Andrea Marco Sartori is a Laravel package for generating enum classes using enum-php.

As shown in the README, let’s say you wanted to build a Status enum. You can use the following command to generate the class and keys:

php artisan make:enum Status 'IN_PROGRESS|COMPLETE|FAILED'

Will make the following Enum class:

<?php
 
namespace App\Enums;
 
use Rexlabs\Enum\Enum;
 
/**
* The Status enum.
*
* @method static self IN_PROGRESS()
* @method static self COMPLETE()
* @method static self FAILED()
*/
class Status extends Enum
{
const IN_PROGRESS = 'in_progress';
const COMPLETE = 'complete';
const FAILED = 'failed';
}

Further, here’s how you define keys and map them to human-readable names:

php artisan make:enum Status 'IN_PROGRESS=1=In progress|COMPLETE=2=Complete|FAILED=3=Failed'

Which generates the following class:

<?php
 
namespace App\Enums;
 
use Rexlabs\Enum\Enum;
 
/**
* The Status enum.
*
* @method static self IN_PROGRESS()
* @method static self COMPLETE()
* @method static self FAILED()
*/
class Status extends Enum
{
const IN_PROGRESS = 1;
const COMPLETE = 2;
const FAILED = 3;
 
/**
* Retrieve a map of enum keys and values.
*
* @return array
*/
public static function map() : array
{
return [
static::IN_PROGRESS => 'In progress',
static::COMPLETE => 'Complete',
static::FAILED => 'Failed',
];
}
}

You can learn more about this package, get full installation instructions, and view the source code on GitHub at cerbero90/laravel-enum.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article