Hi Im using laravel nova 4 and im not able to find any code for dependsOn() method for a dependent field.
I have 2 models (1.type and 2. make)
type belongs to make.
in my resource, field function, i have codes like below
BelongsTo::make('Make'),
BelongsTo::make('Type'),
I want the type dropdown to be dependent on the make selected.
Type has make_id as foreign key.
is there any method i can achieve this.
Thanks for the help in advance.
My Answer:
In Laravel Nova 4, you can achieve a dependent field using the `dependsOn()` method. Here's an example of how you can make the `Type` field dependent on the selected `Make`:
php
BelongsTo::make('Make'),
BelongsTo::make('Type')
->dependsOn('make_id', 'make')
->searchable()
->sortable()
In this code snippet, the `Type` field is dependent on the `make_id` field of the `Make` model. This means that the `Type` field will only show options that are related to the selected `Make`.
Make sure to replace `'make_id'` with the actual foreign key column name in your `Type` model that links it to the `Make` model.
I hope this helps! Let me know if you have any other questions.
Rate this post
5 of 5 based on 3731 votesComments