Blog Detail

30

Sep
Supercharges PHP8-backed Enums with Superpowers in Laravel cover image

arrow_back Supercharges PHP8-backed Enums with Superpowers in Laravel

Laravel Backed Enums is an awesome package that supercharges your PHP8-backed enums with superpowers like localization support and fluent comparison methods.

Installation

composer require webfox/laravel-backed-enums

Usage

Setup your enum

The enum you create must implement the BackedEnum interface and also use the IsBackedEnum trait. The interface is required for Laravel to cast your enum correctly and the trait is what gives your enum its superpowers.

use Webfox\LaravelBackedEnums\BackedEnum;
use Webfox\LaravelBackedEnums\IsBackedEnum;

enum VolumeUnitEnum: string implements BackedEnum
{
    use IsBackedEnum;

    case MILLIGRAMS = "MILLIGRAMS";
    case GRAMS = "GRAMS";
    case KILOGRAMS = "KILOGRAMS";
    case TONNE = "TONNE";
}

Enum value labels (Localization)

Create enums.php lang file and create labels for your enum values.

// resources/lang/en/enums.php

return [
     VolumeUnitEnum::class => [
        VolumeUnitEnum::MILLIGRAMS->value => "mg",
        VolumeUnitEnum::GRAMS->value      => "g",
        VolumeUnitEnum::KILOGRAMS->value  => "kg",
        VolumeUnitEnum::TONNE->value      => "t"
     ]
];

You may then access these localized values using the ->label() or ::labelFor() methods

VolumeUnitEnum::MILLIGRAMS->label(); // "mg"
VolumeUnitEnum::labelFor(VolumeUnitEnum::TONNE); // "t"

If you do not specify a label in the lang file these methods will return the value assigned to the enum inside the enum file. e.g MILLIGRAMS label will be MILLIGRAMS.

Meta data

Adding metadata allows you to return additional values alongside the label and values.

Create a withMeta method on your enum to add metadata.

public function withMeta(): array
{
    return match ($this) {
        self::MILLIGRAMS                => [
            'background_color' => 'bg-green-100',
            'text_color'       => 'text-green-800',
        ],
        self::GRAMS                     => [
            'background_color' => 'bg-red-100',
            'text_color'       => 'text-red-800',
        ],
        self::KILOGRAMS, self::TONNE    => [
            'background_color' => 'bg-gray-100',
            'text_color'       => 'text-gray-800',
        ],
        default                         => [
            'background_color' => 'bg-blue-100',
            'text_color'       => 'text-blue-800',
        ],
    };
}

If you do not specify a withMeta method, the meta will be an empty array.

For more details, visit Github.

Closing Notes

Being a custom web app development company, Codebrisk has a lot of experience developing CRM software, and e-commerce and SASS web applications for various industries. So if you want to build any web application, We are here to assist you with this initiative. Please feel free to get in touch with us or you can get a free quote here.

Published at : 30-09-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project