epartment / nova-dependency-container

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

Use BelongsTo inside NovaDependencyContainer #193

Open daugaard47 opened 2 years ago

daugaard47 commented 2 years ago

1. If I do this:

                Select::make('Description Type', 'description_type')->options([
                        0 => 'Default Description',
                        1 => 'Country Specific Description',
                        2 => 'Destination Specific Description',
                ])->displayUsingLabels(),

                NovaDependencyContainer::make([
                        Trix::make('Default Description', 'default_description'),
                ])->dependsOn('description_type', 0),

This works.

2. If I add BelongsTo::make('Country', 'country'), Inside the NovaDependencyContainer This Does Not Work:

                NovaDependencyContainer::make([
                        Trix::make('Default Description', 'default_description'),
                        BelongsTo::make('Country', 'country'),
                ])->dependsOn('description_type', 0),

The Country list in empty.

If I move BelongsTo::make('Country', 'country'), outside of the NovaDependencyContainer the Country list is populated.

Is there a way to get option 2 to work?

MarcosGit commented 2 years ago

Hi, daugaard47

You can change the BelongsTo to normal select and make your query:

Select::make('Type')->options(function () { return array_filter(Element::pluck('name', 'id')->toArray()); }),

alex-osborn commented 1 year ago

Still occurring.

Another workaround if anyone runs into this. You can get the relationship loading if you tell Nova about it, by registering a top-level field. You can then set it to always be hidden.

// Problem: The BelongsTo won't load in the NovaDepdenencyContainer
// Solution: Add a BelongsTo and force-hide it.
BelongsTo::make('Template', 'template', ChecklistGroupResource::class)
    ->onlyOnDetail()
    ->hideFromDetail(),
NovaDependencyContainer::make([
    BelongsTo::make('Template', 'template', ChecklistGroupResource::class)
])
     ->dependsOn('type', ChecklistGroupType::COMPANY_FROM_TEMPLATE),