Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How can I use PHP Enums in Laravel Custom Casts
    PHP Enums Laravel Custom Casts Eloquent Models Base Enum Class Custom Cast Implementation Laravel Enum Integration Type Safety Enum Methods


How can I use PHP Enums in Laravel Custom Casts


here is how you can use PHP Enums in Laravel Custom Casts:

1. Create a Base Enum Class:
Start by creating a base Enum class that will contain your custom methods. This allows you to reuse the methods across multiple Enum classes.

php
namespace App\Enums;

use BenSampo\Enum\Enum;

abstract class BaseEnum extends Enum
{
    /**
     * Get the display name of the enum value.
     *
     * @return string
     */
    public function getDisplayName(): string
    {
        return $this->description;
    }

    /**
     * Check if the enum value is final.
     *
     * @return bool
     */
    public function isFinal(): bool
    {
        return $this->isFinal;
    }
}

2. Create a Custom Cast that Uses the Enum:
Next, create a Custom Cast that will handle the casting of your Enum values. The Custom Cast should implement the `CastsAttributes` interface.

php
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use App\Enums\ProductStatus;

class ProductStatusCast implements CastsAttributes
{
    public function get($model, $key, $value, $attributes)
    {
        return new ProductStatus($value);
    }

    public function set($model, $key, $value, $attributes)
    {
        return $value->value;
    }
}

3. Use the Custom Cast in Your Model:
In your Eloquent model, add the Custom Cast to the `$casts` property, using the Enum class as the value.

php
use App\Enums\ProductStatus;
use App\Casts\ProductStatusCast;

class Product extends Model
{
    protected $casts = [
        'status' => ProductStatusCast::class,
    ];
}

Now, when you interact with the `status` attribute of your `Product` model, it will automatically be cast to the corresponding `ProductStatus` Enum instance.

php
$product = Product::first();
echo $product->status; // Output: ProductStatus::AVAILABLE

The key benefits of this approach are:

1. Type Safety: By using Enums, you ensure that the status attribute can only be set to a valid value, improving the overall type safety of your application.
2. Readability: Enum values are more expressive and easier to understand than raw database values.
3. Reusability: The Custom Cast can be reused across multiple models that need to work with the same set of Enum values.
4. Custom Functionality: You can add custom methods to the base Enum class, which will be available to all Enum instances.

This integration of PHP Enums and Laravel Custom Casts provides a powerful and flexible way to manage data in your Laravel applications.

Citations:
[1] https://laravelonline.com/articles/exploring-laravel-custom-casts-with-php-enums
[2] https://welovedevs.com/articles/laravel-use-attributes-to-add-superpowers-to-your-enums/
[3] https://laravel-code.tips/php-81-enums-can-be-used-as-model-casts/
[4] https://www.csrhymes.com/2023/07/04/using-php-enums-in-laravel.html
[5] https://laracasts.com/episodes/2328