In this article, we'll create and use enum in laravel 11. An enum, short for "enumeration," is a data type in programming comprising a predefined set of named values, often termed members or elements. It empowers developers to establish a group of related constants, enhancing code readability and maintainability
In Laravel 11, a new Artisan command has been introduced to facilitate the creation of enums. In this example, we'll craft a "UserStatus" enum. This enum will encompass statuses such as Pending, Active, Inactive, and Rejected. We'll demonstrate employing enum attribute casting within a Laravel model for seamless integration.
So, let's see laravel 11 creates an enum, how to create an enum in laravel 11, how to use an enum in laravel 11, Laravel Enum Class, and php enum.
You can create an enum using the following command in laravel 11.
php artisan make:enum {enumName}
In the first step, we'll install laravel 11 using the following command.
composer create-project --prefer-dist laravel/laravel laravel-11-example
After that, we'll create migration using the following command.
php artisan make:migration create_users_table
Migration:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('email');
$table->string('status')->default('pending');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
}
};
Then migrate the table into the database using the following command.
php artisan migrate
In this step, we'll create a UserStatus.php class and define all enum values. So, run the following command to create an enum in laravel 11.
php artisan make:enum Enums/UserStatus
app/Enums/UserStatus.php
<?php
namespace App\Enums;
enum UserStatus: string
{
case Pending = 'pending';
case Active = 'active';
case Inactive = 'inactive';
case Rejected = 'rejected';
}
Now, we'll create a User.php model with casting. So, run the following command to create a model.
php artisan make:model User
App/Models/User.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Enums\UserStatus;
class User extends Model
{
use HasFactory;
/**
* Write code on Method
*
* @return response()
*/
protected $fillable = [
'name', 'email', 'status'
];
/**
* Write code on Method
*
* @return response()
*/
protected function casts(): array
{
return [
'status' => UserStatus::class,
];
}
}
Now, we'll define the routes into the web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'index']);
In this step, we'll create a UserController.php file and define the index() function.
app/Http/Controllers/UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Enums\UserStatus;
use App\Models\User;
class UserController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$input = [
'name' => 'james',
'email' => 'james@test.com',
'status' => UserStatus::Active
];
$user = User::create($input);
dd($user->status, $user->status->value);
}
}
Run the laravel 11 application using the following command:
php artisan serve
You might also like: