Today we are going to learn Enum Model Attribute Casting in Laravel 9 Example. This tutorial will help you to learn how to use enum in laravel 9. You are at a right place where to learn Enum Model Attribute Casting in Laravel 9 Example.
When you are using enum data type for the table then you can create migration and then you can take enum datatype and enum value. Then when you want to add more enum value then you have again create migration and then you have to change it.
So, Laravel then introduces enum model attribute casting. You can create an enum class and set cast on the model.
Note: Enum casting is only available for PHP 8.1+.
Steps for Enum Model Attribute Casting in Laravel 9 Example:
- Step 1: Installing fresh new Laravel 9 Application
- Step 2: Creating Database and Migration
- Step 3: Creating Enum Class
- Step 4: Creating Model
- Step 5: Creating Route
- Step 6: Creating Controller
- Step 7: Testing
- Step 8: Conclusion
Also Read: How to Store JSON in Database Laravel 9
Step 1: Installing fresh new Laravel 9 Application
First, We are going to install a fresh new laravel 9 application. To install a laravel 9 application run the following command in terminal.
composer create-project laravel/laravel enum-app
cd enum-app
Note: “enum-app” is the laravel 9 application name.
Step 2: Creating Database and Migration
Now we are going to create a database. Open phpmyadmin and create a new database with name enum-app.
Enter the database details to .env as shown in below code.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=enum-app
DB_USERNAME=root
DB_PASSWORD=
Now we are going to create products migration with name, body and status columns. To create a migration run the following command in terminal.
php artisan make:migration create_products_table
Also Read: Create React JS CRUD using Vite in Laravel 9 Example
Now, Open the products migration file which we can be found inside database/migrations folder and update the following code
<?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('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('body');
$table->string('status')->default('pending');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
};
Now run the migration to create a products table.
php artisan migrate
Also Read: Datatables Filter with Dropdown in Laravel 9 Example
Step 3: Creating Enum Class
In this step we are going to create ProductStatusEnum.php class inside app/Enums/ folder and enter the following code.
<?php
namespace App\Enums;
enum ProductStatusEnum:string {
case Pending = 'pending';
case Active = 'active';
case Inactive = 'inactive';
case Rejected = 'rejected';
}
Step 4: Creating Model
In this step, We are going to create Product.php model with casting. To create a Product.php enter the following code in terminal.
php artisan make:model Product
and update the following code of app/Models/Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Enums\ProductStatusEnum;
class Product extends Model
{
use HasFactory;
/**
* Write code on Method
*
* @return response()
*/
protected $fillable = [
'name', 'body', 'status'
];
/**
* Write code on Method
*
* @return response()
*/
protected $casts = [
'status' => ProductStatusEnum::class
];
}
Also Read: Ajax File Upload with Progress Bar in Laravel 9 Example
Step 5: Creating Route
So now, we are going to create a route for our application. Open routes/web.php and add the following route as show below.
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ProductController; /* |-------------------------------------------------------------------------- | 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('product-test', [ProductController::class, 'index']);
Step 6: Creating Controller
In our last step, we are going to create a ProductController.php file inside app/Http/Controller/ and update the following as show below.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class ProductController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$input = [
'name' => 'Gold',
'body' => 'This is a Gold',
'status' => ProductStatusEnum::Active
];
$product = Product::create($input);
dd($product->status, $product->status->value);
}
}
Step 7: Testing
So now, Its time to test our application. To start a laravel server run the following code in terminal.
php artisan serve
and enter the following URL in any web browser.
http://127.0.0.1:8000/product-test
You can see database output and print variable output:
Database Output:

Output:
App\Enums\ProductStatusEnum {#1250
name: "Active"
value: "active"
}
active
Step 8: Conclusion
Today, We had learn Enum Model Attribute Casting in Laravel 9 Example. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.
Also Read: How to Create RSS Feed in Laravel 9 Example
[…] Also Read: Enum Model Attribute Casting in Laravel 9 Example […]
[…] Also Read: Enum Model Attribute Casting in Laravel 9 Example […]
[…] Also Read: Enum Model Attribute Casting in Laravel 9 Example […]