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.