Where to put/how to handle enums in Laravel?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
- Where to put enums?
- Making them globally accessible?
To maintain consistency across your project, it is recommended to store your enums within a separate file. This approach ensures that you can easily find and access them whenever needed. Create a folder called 'Enums' or something similar in your Laravel application root directory and place all enum data structures inside this folder.
// Enums/PhoneTypes.php:
$phoneTypes = [
'CELL' => "Cellular",
'HOME' => "Home",
'WORK' => "Work",
];By keeping all enums stored together, you can easily find the required data without any hassle.
To use enums in multiple places across your application, including both views and the database, follow these steps:
1. Ensure you have installed and configured Laravel's Artisan commands: `composer require laravel/artisan` and set appropriate permissions for the 'vendor/bin' folder. 2. Create a helper function in your app's 'Helper' directory, which will load all enums from their respective files. This function can be called as follows:function enum($enum_key) {
$enum_obj = App\Enums\$enum_key;
return isset($enum_obj->{$enum_key}) ? $enum_obj->{$enum_key} : null;
}use App\Enums\PhoneTypes;
use Illuminate\Support\Facades\Artisan;
public function boot() {
Artisan::add(new EnumCommand());
}namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Enums\PhoneTypes;
use App\Helpers\EnumHelper;
class EnumCommand extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'enum:load';
public function handle() {
foreach (glob(storage_path('app/Enums/*.php')) as $file) {
require_once($file);
}
}
}php artisan enum:loadNow, you can access your enums globally in any part of your Laravel application using the enum helper function:
echo EnumHelper::enum('CELL'); // Output: Cellular