
Introduction
Many programming languages have a specific data type called enums(enumerations), that lets a variable be any of a predetermined set of constants. PHP 8.1 saw the introduction of enums, which provide you the ability to declare a group of named values that may be used as type-safe constants in your code
1. Install Laravel
- You can use the following command to launch a Laravel project if you haven't before.
composer create-project laravel/laravel laravel_tutorial --prefer-dist
2. Create the Enum class:
- Create a new PHP file inside the app/Enums directory, for example, StatusEnum.php. This file will contain the enum constants and their associated values:
<?php
namespace App\Enums;
enum StatusEnum: string {
Case Pending = 'pending';
Case Active = 'active';
Case Inactive = 'inactive';
public static function getValues() : array
{
return array_column(self::cases(), 'value');
}
public static function getNames(): array
{
return array_column(self::cases(), 'name');
}
public static function toArray(): array
{
return array_combine(self::getNames(), self::getValues());
}
}
3. Enum Casting in the Model:
- Laravel provides a useful feature that allows you to cast attribute values as enums directly within the relevant model.
<?php
namespace App\Models;
use App\Enums\StatusEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
protected $fillable = [
'name',
'email',
'password',
'status'
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'status' => StatusEnum::class,
];
}
4. Database Migration:- In your database migration files, you can define ENUM columns using the ->enum() method:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Enums\StatusEnum;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
//…
$table->enum('status', StatusEnum::getValues() )->default(StatusEnum::Pending->value);
});
}
};
Run the “php artisan migrate” command to update the status columns in the user table.
5. Retrieve the enum values:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Enums\StatusEnum;
class UserController extends Controller
{
public function index() {
$status = StatusEnum::toArray();
return view('user.index', compact('status'));
}
}
6. Validate Enum Values.
- In this example, we will demonstrate how to validate a form field using enum values. Enums are a powerful way to define a set of named constants, which can be used to enforce specific, valid values for a given field.
<?php
use App\Enums\StatusEnum;
use Illuminate\Validation\Rules\Enum;
public function store(Request $request)
{
$request->validate([
'status' => ['required', new Enum(StatusEnum::class)],
]);
// Proceed with storing the data
}
Thanks for reading.
* Please Don't Spam Here. All the Comments are Reviewed by Admin.