Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

heppi75's avatar

Possible to get form data from dependsOn outside over the request

I'm currently facing the problem of passing a value from a BelongTo field to a function in a nova resource.

BelongsTo::make(__('Customer'), 'customer', Customer::class)->hideFromIndex()

// I can grab in
Hidden::make('selected_customer')->dependsOn('customer', function ($field, NovaRequest $request, $formData) {
	$field->setValue($formData->customer);
}),

// what I need would be somethin like this - can I get this somewhere, maybe in the nova request? Can't find it ...
new Panel('My Panel', $this->selectionFields($formData->customer)),

Maybe some more experienced nova developer can give me a note on this. Or how could it possibly be solved alternatively?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It seems like you're trying to access the value of a BelongsTo field within a function in a Nova resource, and you want to use this value to conditionally display fields in a panel. Unfortunately, Nova does not directly pass the $formData to the panel or fields outside of the dependsOn closure.

However, you can achieve a similar result by using a computed field or a custom tool to manipulate the Nova UI based on the selected value. Here's a possible solution using a computed field:

use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\Hidden;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Panel;

BelongsTo::make(__('Customer'), 'customer', Customer::class)
    ->hideFromIndex(),

Hidden::make('Selected Customer', 'selected_customer')
    ->withMeta(['value' => $this->customer_id])
    ->onlyOnForms(),

// Computed field to dynamically generate the panel fields
$this->dynamicPanel(),

// ...

// In your Nova resource, add a method to create the dynamic panel
protected function dynamicPanel()
{
    return new Panel('My Panel', $this->selectionFields($this->customer_id));
}

// Then, define the selectionFields method
protected function selectionFields($customerId)
{
    // You can now use $customerId to conditionally add fields to the panel
    $fields = [];

    if ($customerId) {
        // Add your fields based on the selected customer
        // $fields[] = ...
    }

    return $fields;
}

In this example, we're using a Hidden field to store the selected customer's ID, which is then used in a computed field method (dynamicPanel) to generate the panel fields. The selectionFields method is where you would conditionally add fields to the panel based on the selected customer.

Please note that this solution assumes that the customer_id is available on the resource model. If the customer_id is not directly available, you may need to adjust the code to fetch the correct value.

Keep in mind that this is a workaround and may not cover all use cases. Nova's UI is not as dynamic as some other admin panels, so if you need more complex dynamic behavior, you might need to create a custom tool or use JavaScript to enhance the UI.

1 like

Please or to participate in this conversation.