定制 emilianotisato/nova-belongsto-depends 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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
  1. Select a Warehouse and get all articles of the warehouse
  2. 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

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 15.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 63
  • 开发语言: Vue

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-02-06