simplesquid / nova-enum-field

An enum field and filters for Laravel Nova.
MIT License
52 stars 26 forks source link

Can we apply the enum resolution on the index? #21

Closed Riaan-ZA closed 3 years ago

Riaan-ZA commented 3 years ago

Currently on the index listing page it just outputs the number, is it possible to show the resolved value there?

mdpoulter commented 3 years ago

Hi @Riaan-ZA. This should already be the case, as can be seen here: https://github.com/simplesquid/nova-enum-field/blob/a059de0a2016487fb11853bdc49a458be925555f/src/Enum.php#L30-L33

Could you perhaps share more details of your setup?

Riaan-ZA commented 3 years ago

I have

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

in my config and

'user_type' => UserType::class,

in the $casts array in my model.

It resolves the values correctly in the forms, but on the listing, as well as the detail (which I just noticed) it's just ouputing the number value:

image

I'm using nova 3.15.0 and laravel 8

mdpoulter commented 3 years ago

That's strange 🤔 I'll look into why it's doing that and get back to you.

martio commented 3 years ago

Hi,

Instead of this:

    protected $casts = [
        'user_type' => UserType::class,
    ];

should be:

    protected $enumCasts = [
        'user_type' => UserType::class,
    ];

    protected $casts = [
        'user_type' => 'integer',
    ];

?

Riaan-ZA commented 3 years ago

I don't think that's correct as least that's not working for me, both these docs and BenSampo docs say otherwise.

I does seem like there something wrong with my attribute casting though, since the return value is an integer and not an instance of \BenSampo\Enum\Enum

mdpoulter commented 3 years ago

I think I've figured out the issue - just working on a solution now. We may have inadvertently introduced a breaking change in v2, so will probably have to release the fix with v3 (as the fix will then be a breaking change too). I'll keep you updated.

Riaan-ZA commented 3 years ago

I've managed to get it fixed my side, the main issue was I wasn't using the BenSampo\Enum\Traits\CastsEnums trait. After using that in combination with your suggestion above, it works.

Riaan-ZA commented 3 years ago

Also tested just having:

protected $casts = [
        'email_verified_at' => 'datetime',
        'type' => UserType::class,
    ];

which works,

So not using the trait was my mistake

mdpoulter commented 3 years ago

You're absolutely right! Let me update the README to include that, as I see I was doing the same thing from a fresh install... 😴

mdpoulter commented 3 years ago

I have discovered a few things I'm going to change for v3 along the way, but at least we know it's not actually a bug!