PHP code example of rickgoemans / laravel-enum-helpers

1. Go to this page and download the library: Download rickgoemans/laravel-enum-helpers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rickgoemans / laravel-enum-helpers example snippets




namespace Rickgoemans\LaravelEnumHelpers\Enums;

use Rickgoemans\LaravelEnumHelpers\Traits\HasEnumHelpers;

enum CreditCheckRating: string
{
    use HasEnumHelpers;

    case AAA = 'AAA';
    case AA = 'AA';
    case A = 'A';
    case BBB = 'BBB';
    case BB = 'BB';
    case B = 'B';
    case CCC = 'CCC';
    case CC = 'CC';
    case C = 'C';
    case D = 'D';

    /** @inheritdoc */
    public static function badgeColors(): array
    {
        return [
            self::AAA->label() => 'success',
            self::AA->label()  => 'success',
            self::A->label()   => 'success',
            self::BBB->label() => 'info',
            self::BB->label()  => 'info',
            self::B->label()   => 'info',
            self::CCC->label() => 'info',
            self::CC->label()  => 'info',
            self::C->label()   => 'info',
            self::D->label()   => 'danger',
            self::NR->label()  => 'danger',
            null               => 'danger',
        ];
    }

    /** @inheritdoc */
    public static function order(): array
    {
        return [
            self::AAA,
            self::AA,
            self::A,
            self::BBB,
            self::BB,
            self::B,
            self::CCC,
            self::CC,
            self::C,
            self::D,
            self::NR,
        ];
    }
    
    /** @inheritdoc */
    public function label(): string
    {
        return $this->baseLabel('credit_checks.rating.values.');
    }
}



namespace App\Nova;

use Laravel\Nova\Fields\Badge;
use Laravel\Nova\Resource;
use Rickgoemans\LaravelEnumHelpers\Enums\CreditCheckRating;

class CreditCheck extends Resource {
    // ...
    
    public function fields(Request $request): array {
        return [
            // ...
            
            Badge::make(__('credit_checks.rating.label'), 'rating', fn(CreditCheckRating $rating) => $rating->label())
                ->map(CreditCheckRating::badgeColors())
                ->sortable(),
                
            Select::make(__('credit_checks.rating.label'), 'rating')
                ->options(CreditCheckRating::optionsForSelect())
                ->onlyOnForms(),
                
            // ...
        ];       
    }
    
    // ...
}