In this article explain of how to use enum in Laravel. This post is focused on Laravel enum attribute casting example. We will help you to give example of how to use enum in Laravel.
If you want to use the enum data type for table then you can create migration and take the enum data type and set enum value in migration. When later you want to add one more enum value then you need to create again migration and change it.
Enums in Laravel are a clean method to define set of constant values. You can use native enumeration in your Laravel projects to replace constants or even manage specific states.
In this example, first, we will create migration file with string column “status” and default value will be “pending”. Then we will create a model with a casting to set enum class. Then create enum class with specific values and use on model cast. Let’see example and learn.
Follow Below step to integrate Enum in Laravel
1. Create migration using Laravel
Here, We will create migaration for “Posts” table with title, description and status columns and also we will create model for posts table.
php artisan make:migration create_posts_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('description');
$table->string('status')->default('pending');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
Then run migration command to create posts table.
php artisan migrate
2. Create Enum Class
In this step, we will create PostStatusEnum.php class define all enum values. let’s create model and update following code.
app/Enums/PostStatusEnum.php
<?php
namespace App\Enums;
enum PostStatusEnum:string {
case Pending = 'Pending';
case Active = 'active';
case Inactive = 'inactive';
case Rejected = 'rejected';
}
3: Create Model
In this step, we will create Post.php model with casting. Let’s create model following command.
php artisan make:model Post
app/Models/Post.php
<?php
namespace App\Models;
use App\Enums\PostStatusEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = ['title', 'description', 'status'];
protected $casts = [
'status' => PostStatusEnum::class
];
}
4. Create Route
We will create one route for testing enum in laravel. So create one route here.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('post-test', [PostController::class, 'index']);
5. Create Controller
In this step, we will create PostController file and write index() method to create post record.
php artisan make:controller PostController
<?php
namespace App\Http\Controllers;
use App\Enums\PostStatusEnum;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index()
{
$input = [
'title' => 'This is good post',
'description' => 'This is best post by webstuffsolution.com',
'status' => PostStatusEnum::Active
];
$post = Post::create($input);
dd($post->status->value);
}
}
Output:
"active"
I hope this tutorial help you.