Open figurluk opened 2 years ago
@orlyapps how about filterable with dependsOn?
I found a repository that compacts this process: https://github.com/webparking/nova-belongs-to-dependency
A better example (relatableQueryUsing):
BelongsTo::make('Unternehmen', 'company', Company::class)->rules('required'),
BelongsTo::make('Bereich', 'department', Department::class)
->dependsOn(['company'], function (BelongsTo $field, NovaRequest $request, FormData $data) {
$field->relatableQueryUsing(function ($request, $query) use ($data) {
return $query->where('company_id', $data->company);
});
})
->nullable(),
BelongsTo::make('Niederlassung', 'location', Location::class)
->dependsOn(['company'], function (BelongsTo $field, NovaRequest $request, FormData $data) {
$field->relatableQueryUsing(function ($request, $query) use ($data) {
return $query->where('company_id', $data->company);
});
})
->nullable(),
BelongsTo::make('Standort', 'place', Place::class)
->dependsOn(['location'], function (BelongsTo $field, NovaRequest $request, FormData $data) {
$field->relatableQueryUsing(function ($request, $query) use ($data) {
return $query->where('location_id', $data->location);
});
})
->nullable(),
When was relatableQueryUsing
introduced? My Nova app is still on 4.13.0 because every Nova update breaks stuff. relatableQueryUsing
seems to be missing there. Right now I can not invest all those hours for testing after performing an update.
use Illuminate\Database\Eloquent\Builder;
use Laravel\Nova\Fields\FormData;
...
BelongsTo::make('Owner', 'user', User::class)->dependsOn('organization', function(BelongsTo $field, NovaRequest $request, FormData $formData) {
$field->relatableQueryUsing(function (NovaRequest $request, Builder $query) use ($formData) {
$query->when($formData->organization, function ($query) use ($formData) {
$query->where('organization_id', $formData->organization);
});
});
})->nullable(),
Chiming in with a complete solution in Nova 4, very close to @orlyapps but I use when()
which is necessary for my use case. Included imports to save time iterating, since I always forget those.
dependsOn is now built into nova 4 and you can do this. This package will no longer be maintained for nova 4.