php 🚀
note2self 🚀
laravel 🚀
Enums, Casting and Models

Note to self on this one.

When I want to enforce a model field I can easily use the Enum class type.

In the model class I can cast the field to the enum.

    protected $casts = [
        'type' => PaymentEnum::class,

And the Enum looks like


namespace App\Models\Enums;

enum PaymentEnum: string
    case Refund = 'refund';
    case Payment = 'payment';

Now I can forse this to be of these types.

    public function test_enum()
        $model = Payment::factory()->create(['type' => 'foo']);