emilianotisato/nova-belongsto-depends
最新稳定版本:0.1.4
Composer 安装命令:
composer require emilianotisato/nova-belongsto-depends
包简介
A Laravel Nova field with dependencie on another BelongsTo relationship.
README 文档
README
This package is based on the original Orlyapps package, which we do not know if is still under maintenance.
Installation
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'), ]; }
Sample
- Warehouse hasMany Articles
- Articles belongsToMany Suppliers
- Suppliers belongsToMany Articles
- Select a Warehouse and get all articles of the warehouse
- Select a Article and get all suppliers who has this article
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'), ]; }
Use multiple instances
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'),
Credits
- Creator: Orlyapps
- Mantainers: Emiliano Tisato
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 15.47k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-02-06