hypermetrica / nova-belongsto-depend
A Laravel Nova field.
dev-master
2021-12-15 20:38 UTC
Requires
- php: >=7.1.0
This package is auto-updated.
Last update: 2025-02-16 03:28:29 UTC
README
Installation
You can install the package in to a Laravel app that uses Nova via composer:
composer require hypermetrica/nova-belongsto-depend
Use this field in your Nova Resource
use Hypermetrica\NovaBelongsToDepend\NovaBelongsToDepend;
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name')->rules('required', 'max:255'),
NovaBelongsToDepend::make('Company')
->placeholder('Optional Placeholder') // Add this just if you want to customize the placeholder
->options(fn() => Company::all()),
NovaBelongsToDepend::make('Department')
->placeholder('Optional Placeholder') // Add this just if you want to customize the placeholder
->optionsResolve(function ($company) {
// Reduce the amount of unnecessary data sent
return $company->departments()->get(['id','name']);
})
->dependsOn('Company'),
NovaBelongsToDepend::make('Location')
->placeholder('Optional Placeholder') // Add this just if you want to customize the placeholder
->optionsResolve(function ($company) {
// Reduce the amount of unnecessary data sent
return $company->locations()->get(['id','name']);
})
->fallback(
Text::make('Location Name')->rules('required', 'max:255'),
)
->hideLinkToResourceFromDetail()
->hideLinkToResourceFromIndex()
->dependsOn('Company'),
];
}
Options
```openDirection('top') ```
See options values from [vue-multiselect ](https://vue-multiselect.js.org/#sub-props)
## Translation
The following strings are translatable (add then in your language file located in resources/lang/vendor/nova/\*.json).
- 'Oops! No elements found. Consider changing the search query.'
- 'List is empty'
- 'Select'
- 'Press enter to select'
- 'Selected'
- 'Press enter to remove'
## Sample
[Demo Project](https://github.com/orlyapps/laravel-nova-demo)
- Warehouse hasMany Articles
- Articles belongsToMany Suppliers
- Suppliers belongsToMany Articles
1. Select a **Warehouse** and get all articles of the warehouse
2. Select a **Article** and get all suppliers who has this article
```php
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name')->rules('required', 'max:255'),
NovaBelongsToDepend::make('Warehouse')
->options(fn() => Warehouse::all())
->rules('required'),
NovaBelongsToDepend::make('Article')
->optionsResolve(function ($warehouse) {
return $warehouse->articles;
})
->dependsOn('Warehouse')
->rules('required'),
NovaBelongsToDepend::make('Supplier')
->optionsResolve(function ($article) {
return Supplier::whereHas('articles', function ($q) use ($article) {
$q->where('article_id', $article->id);
})->get();
})
->dependsOn('Article')
->rules('required'),
];
}
```
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.