Skip to content

theideabureau/laravel-nova-enum-field

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Nova Enum Field

Nova field for enum in PHP 8.1 and above

Installation

You can install this package in a Laravel app that uses Nova via composer:

composer require theideabureau/laravel-nova-enum-field

Setup

use App\Enums\UserType;
use Illuminate\Database\Eloquent\Model;

class Example extends Model
{
    protected $casts = [
        'user_type' => UserType::class,
    ];
}

Usage

You can use the Enum field in your Nova resource like this:

namespace App\Nova;

use App\Enums\UserType;
use TheIdeaBureau\EnumField\Enum;

class Example extends Resource
{
    // ...

    public function fields(Request $request)
    {
        return [
            // ...

            Enum::make('User Type')->attach(UserType::class),

            // ...
        ];
    }
}

Filters

If you would like to use the provided Nova Select filter (which is compatible with both the Enum and FlaggedEnum fields), you can include it like this:

namespace App\Nova;

use App\Enums\UserPermissions;
use App\Enums\UserType;
use TheIdeaBureau\EnumField\EnumFilter;

class Example extends Resource
{
    // ...

    public function filters(Request $request)
    {
        return [
            EnumFilter::make(__('User Type'), 'user_type', UserType::class),

             // With optional default value:
            EnumFilter::make(__('User Type'), 'user_type', UserType::class, UserType::Administrator),
        ];
    }
}

Alternatively, you may wish to use the provided Nova Boolean filter (which is also compatible with both the Enum and FlaggedEnum fields):

namespace App\Nova;

use App\Enums\UserPermissions;
use App\Enums\UserType;
use TheIdeaBureau\EnumField\EnumBooleanFilter;

class Example extends Resource
{
    // ...

    public function filters(Request $request)
    {
        return [
            EnumBooleanFilter::make(__('User Type'), 'user_type', UserType::class),

            // With optional default values:
            EnumBooleanFilter::make(__('User Type'), 'user_type', UserType::class, [
                UserType::Administrator,
                UserType::Moderator,
            ]),
        ];
    }
}

Credits

Thanks

License

The MIT License (MIT). Please see License File for more information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%