A Laravel Nova field with dependencie on another BelongsTo relationship.
emilianotisato/nova-belongsto-depends is a Laravel package for a laravel nova field with dependencie on another belongsto relationship..
It currently has 2 GitHub stars and 15.763 downloads on Packagist (latest version 0.1.4).
Install it with composer require emilianotisato/nova-belongsto-depends.
Discover more Laravel packages by emilianotisato
or browse all Laravel packages to compare alternatives.
Last updated
This package is based on the original Orlyapps package, which we do not know if is still under maintenance.
You can install the package in to a Laravel app that uses Nova via composer:
composer require emilianotisato/nova-belongsto-depends
Use this field in your Nova Resource
// Add the use statement a the top of your nova resource
use EmilianoTisato\NovaBelongsToDepends\NovaBelongsToDepends;
// ...
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name')->rules('required', 'max:255'),
NovaBelongsToDepends::make('Company')
->options(\App\Company::all()),
NovaBelongsToDepends::make('Department')
->optionsResolve(function ($company) {
// Reduce the amount of unnecessary data sent
return $company->departments()->get(['id','name']);
})
->dependsOn('Company'),
NovaBelongsToDepends::make('Location')
->optionsResolve(function ($company) {
// Reduce the amount of unnecessary data sent
return $company->locations()->get(['id','name']);
})
->dependsOn('Company'),
];
}
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name')->rules('required', 'max:255'),
NovaBelongsToDepends::make('Warehouse')
->options(\App\Warehouse::all())
->rules('required'),
NovaBelongsToDepends::make('Article')
->optionsResolve(function ($warehouse) {
return $warehouse->articles;
})
->dependsOn('Warehouse')
->rules('required'),
NovaBelongsToDepends::make('Supplier')
->optionsResolve(function ($article) {
return \App\Supplier::whereHas('articles', function ($q) use ($article) {
$q->where('article_id', $article->id);
})->get();
})
->dependsOn('Article')
->rules('required'),
];
}
Some times you need to use multiple instances o mabye you have a second resource for the same eloquent model (example: App\Nova\User and App\Nova\Provider both represent App\User model). In those cases to avoid conflict you can delcare the specific Nova resources in meta attributes (->withMeta(['calledFromClass' => 'App\Nova\Provider'])) like this:
NovaBelongsToDepends::make('Client')
->options(\App\Client::all()),
NovaBelongsToDepends::make('Provider')
->withMeta(['calledFromClass' => 'App\Nova\Provider'])
->optionsResolve(function ($client) {
return $client->providers()->active()->get(['id', 'name']);
})->dependsOn('Client')
->rules('required'),
The MIT License (MIT). Please see License File for more information.