epartment / nova-dependency-container

A Laravel Nova field container allowing to depend on other fields values
MIT License
382 stars 163 forks source link

Enum cannot be converted to int #99

Open smualex opened 4 years ago

smualex commented 4 years ago

Enums/ContactType.php:

<?php

namespace App\Enums;

use BenSampo\Enum\Enum;

/**
 * @method static static Default()
 * @method static static Student()
 * @method static static Alumni()
 */
final class ContactType extends Enum
{
    const Default =   0;
    const Student =   1;
    const Alumni = 2;
}

Contact model:

use BenSampo\Enum\Traits\CastsEnums;
...

class Contact extends Model
{
    use CastsEnums;

    protected $enumCasts = [
        'contact_type' => ContactType::class,
    ];
...

Database:

use App\Enums\ContactType;

....

$table->integer('contact_type')->nullable()->default(ContactType::Default);

Finally in my Nova model:

            Enum::make('Contact Type')->attachEnum(ContactType::class)->nullable(),

            NovaDependencyContainer::make([
                    Boolean::make('International Student'),
                    NovaDependencyContainer::make([
                        Text::make('Country of Origin', 'country')->hideFromIndex(),
                    ])->dependsOn('international_student', true),                        
                Date::make('Graduation')->hideFromIndex(),   
            ])->dependsOn('contact_type', ContactType::Student),

This works when creating a new resource, the dependency works perfectly.

When loading the resource page, it throws the error: Object of class App\Enums\ContactType could not be converted to int

When I change the dependsOn line to this: dependsOn('contact_type', ContactType::Student())

The page loads properly but the dependency check is now broken. If I select "Student" from my drop down it does not load the other fields. Any thoughts around this?

I am using this package for Enums: https://github.com/BenSampo/laravel-enum and this package for my Nova field: https://github.com/simplesquid/nova-enum-field

wize-wiz commented 4 years ago

@smualex Thanks for the time to post this. I have to install both packages to see how it handles the vue part. I'm very familiar with enums but not with these packages or even an approach to implement/emulate this behaviour in PHP.

Will take some time but I'll get back on this issue soon.

LoganTFox commented 4 years ago

I am also having this same issue.

pbogomyagkov commented 4 years ago

same issue. any updates?

patrickjmeurer commented 3 years ago

Any updates?

ragingdave commented 3 years ago

Can you provide the stacktrace around the error? I'm looking to try and fast track a fix for this, but I don't have a working setup so I that would be helpful

patrickjmeurer commented 3 years ago

The error occurs when you try to edit some resource. If you are using the enum value ContactType::Student on dependency check, give a error Enum cannot be converted to int, but if uses the enum function ContactType::Student(), works properly, at least for me.

What i see, is the DependencyContainer is triyng to convert an object to integer when is passed a integer (or string) value to him, idk.

I'm going to investigate more about this, and send a PR to fix that.

ragingdave commented 3 years ago

cool would gladly appreciate the PR @potreco!