GithubHelp home page GithubHelp logo

korobkovandrey / nova-enum-field Goto Github PK

View Code? Open in Web Editor NEW

This project forked from datomatic/nova-enum-field

0.0 1.0 0.0 197 KB

Laravel Nova enum field and filters with datomatic/enum-helper support

License: MIT License

PHP 100.00%

nova-enum-field's Introduction

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

Laravel Nova Enum Field

Nova field for enums in PHP 8.1 and above (both pure Enum and BackedEnum) with datomatic/enum-helper and datomatic/laravel-enum-helper compatibility.

Select field on form

There is also a Nova Select filter and Nova Boolean filter: Select filter Boolean filter

Installation

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

composer require datomatic/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 Datomatic\Nova\Fields\Enum\Enum;

class Example extends Resource
{
    // ...

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

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

            // ...
        ];
    }
}

Be aware that order in which methods on the field are called can be sigificant. For example nullable() must be called before before attach(), and options() must be called after attach().

If you use datomatic/laravel-enum-helper you can set optionally a custom dynamic property or/and a subset of cases.
The default property is description.

Enum::make('User Type','user_type')
    ->nullable()
    ->property('excerpt')
    ->cases([UserType::ADMINISTRATOR,UserType::MODERATOR])
    ->attach(UserType::class),

Filters

If you would like to use the provided Nova Select filter, you can include it like this:

namespace App\Nova;

use App\Enums\UserPermissions;
use App\Enums\UserType;
use Datomatic\Nova\Fields\Enum\EnumFilter;

class Example extends Resource
{
    // ...

    public function filters(Request $request)
    {
        return [
            EnumFilter::make('user_type', UserType::class),
                
             // With optional name and default value:
            EnumFilter::make('user_type', UserType::class)
                ->name(__('User Type'))
                ->default(UserType::ADMINISTRATOR)
        ];
    }
}

Alternatively, you may wish to use the provided Nova Boolean filter:

namespace App\Nova;

use App\Enums\UserPermissions;
use App\Enums\UserType;
use Datomatic\Nova\Fields\Enum\EnumBooleanFilter;

class Example extends Resource
{
    // ...

    public function filters(Request $request)
    {
        return [
            EnumBooleanFilter::make('user_type', UserType::class),
                
            // With optional name and default value:
            EnumBooleanFilter::make('user_type', UserType::class)
                ->name(__('User Type'))
                ->default([UserType::ADMINISTRATOR, UserType::MODERATOR])
        ];
    }
}

If you use datomatic/laravel-enum-helper you can set optionally a custom dynamic property or/and a subset of cases.
The default property is description.

// Enum filter
EnumFilter::make('user_type', UserType::class)
    ->name('User Type')
    ->property('excerpt')
    ->cases([UserType::ADMINISTRATOR,UserType::MODERATOR])
// Boolean Enum filter
EnumBooleanFilter::make('user_type', UserType::class)
    ->name('User Type')
    ->property('excerpt')
    ->cases([UserType::ADMINISTRATOR,UserType::MODERATOR])

Credits

Thanks

License

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

nova-enum-field's People

Contributors

trippo avatar andreschwarzer avatar synchro avatar suleymanozev avatar bohemima avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.