epartment / nova-dependency-container

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

BelongsTo dependency for other than primary key #133

Open ExFredrik opened 4 years ago

ExFredrik commented 4 years ago

I'm trying to create an dependancy based on a string-value from a BelongsTo-table. Is this supposed to work or are there any possible sollutions for this?

Similiar to issue: #114

Code example: BelongsTo::make('Produkt', 'product', 'App\Nova\Product'),

NovaDependencyContainer::make([ Date::make('Startdatum', 'start_date')
]) ->dependsOn('product.StringCollumn', 'ValueOfStringCollumn')

sanderbaas commented 4 years ago

Is there any update for this?

davi5e commented 3 years ago

The interesting thing is that the code should handle any property as the column name is saved as $dependency['property']}.

Regardless, it only works for the primary key. Maybe it's some restriction on what the frontend can "see" about the resource being edited?

midorikocak commented 3 years ago

Looking for this.

cwilby commented 2 years ago

If the PR above is merged, you'll soon be able to reference BelongsTo fields like so:

NovaDependencyContainer::make([
  Boolean::make('Admin')
])->dependsOn('user.type', 'admin');
mathieufrh commented 2 years ago

As a workaround I have made a change to allow using array as dependsOn value. And I use it to bypass this issue like so:

    public function fields(Request $request)
    {
        return $this->superchargeFields(
            [
                BelongsTo::make('AWX Variable', 'awx_variable'),

                NovaDependencyContainer::make([Text::make('Value')])
                    ->dependsOnArray(
                        'awx_variable.id',
                        AwxVariable::where('var_type', 'text')->pluck('id')->toArray()
                    ),

                NovaDependencyContainer::make([TextArea::make('Value')])
                    ->dependsOnArray(
                        'awx_variable.id',
                        AwxVariable::where('var_type', 'textarea')->pluck('id')->toArray()
                    ),

                NovaDependencyContainer::make([Number::make('Value')])
                    ->dependsOnArray(
                      'awx_variable.id',
                      AwxVariable::where('var_type', 'number')->pluck('id')->toArray()
                  ),

                ...
            ]
    }

Check this release : https://github.com/mathieufrh/nova-dependency-container/releases/tag/1.3.6