alexwenzel / nova-dependency-container

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

dependsOn with custom column (instead of id) #10

Closed nbartokos closed 3 months ago

nbartokos commented 1 year ago

I have two tables called ActionTypes and RunActions.

RunActions has a column action_type_id. ActionTypes has a column field.

In App\Models\RunAction:

public function actionType() {
    return $this->belongsTo(\App\Models\ActionType::class);
}

In App\Nova\RunAction:

public function fields(NovaRequest $request)
{
    $x = \App\Models\RunAction::find($request->resourceId);
    $y = $x->actionType->field_label;

    return [
        BelongsTo::make('Post'),
        BelongsTo::make('Action Type'),

        DependencyContainer::make([
            Text::make($y, 'content')
        ])->dependsOn('actionType.field', 't'),

        Date::make('Happened When')
    ];
}

The select-field for "Action Type" is displayed correctly, but I never see the dependency-container. I have tried the following strings:

But if I use: ->dependsOn('actionType, 4), with the given ID, everything works. Unfortunately I want to use the "field"-column, but this doesn't work.

MY WORKAROUND FOR NOW:

I have created two functions to return me an array with the IDs for all rows with the give "field" (type). With ->dependsOnIn('actionType', $this->getTypeIds('t')); everything works fine.

public function fields(NovaRequest $request)
{
    return [
        BelongsTo::make('Post'),
        BelongsTo::make('Action Type'),

        DependencyContainer::make([Text::make($this->getTypeLabel('t'), 'content')])->dependsOnIn('actionType', $this->getTypeIds('t')),
        DependencyContainer::make([Number::make($this->getTypeLabel('n'), 'content')])->dependsOnIn('actionType', $this->getTypeIds('n')),
        DependencyContainer::make([Boolean::make($this->getTypeLabel('b'), 'content')])->dependsOnIn('actionType', $this->getTypeIds('b')),

        Date::make('Happened When')
    ];
}

public function getTypeIds($type) {
    return \App\Models\ActionType::where('field', $type)->pluck('id');
}
public function getTypeLabel($type) {
    return \App\Models\ActionType::where('field', $type)->field_label;
}

Big thanks to Alex, to answer me so fast via e-mail and suggest me to create this issue.

DuyenNguyen66 commented 1 year ago

Thank Nbartokos, it works!