iamgerwin/nova-dependency-container
Composer 安装命令:
composer require iamgerwin/nova-dependency-container
包简介
A Laravel Nova 4 and 5 field container allowing to depend on other fields values
README 文档
README
A Laravel Nova field container allowing fields to depend on other field values. Show and hide fields dynamically based on other fields' values with support for complex conditional logic.
Features
- Conditional Field Display: Show/hide fields based on other field values
- Multiple Dependency Types: Support for various comparison operators
- Complex Logic: Chain multiple conditions together
- Flexible Field Support: Works with whitecube/nova-flexible-content layouts
- Nova 4 & 5 Compatible: Works with Laravel Nova 4.x and 5.x (tested with Nova 4.35.x and Nova 5.7.5)
- Laravel 12 Ready: Full support for Laravel 11.x and 12.x
- PHP 8.3 Support: Modern PHP features and type safety
- Fully Tested: Comprehensive test coverage with Pest
- Development Ready: Comprehensive testing and code quality tools
Requirements
- PHP 8.3 or higher
- Laravel 11.x or 12.x
- Laravel Nova 4.x or 5.x
Installation
You can install the package via composer:
composer require iamgerwin/nova-dependency-container
Usage
Basic Usage
use Iamgerwin\NovaDependencyContainer\NovaDependencyContainer; use Laravel\Nova\Fields\Select; use Laravel\Nova\Fields\Text; use Laravel\Nova\Fields\Textarea; public function fields(NovaRequest $request) { return [ Select::make('Type') ->options([ 'personal' => 'Personal', 'business' => 'Business', ]), NovaDependencyContainer::make([ Text::make('Company Name'), Text::make('Tax ID'), ])->dependsOn('type', 'business'), NovaDependencyContainer::make([ Text::make('Personal ID'), Text::make('Date of Birth'), ])->dependsOn('type', 'personal'), ]; }
Available Dependency Methods
dependsOn(string $field, $value)
Show container when field equals specific value:
NovaDependencyContainer::make([ Text::make('Company Name'), ])->dependsOn('type', 'business')
dependsOnIn(string $field, array $values)
Show container when field value is in array:
NovaDependencyContainer::make([ Text::make('Priority Note'), ])->dependsOnIn('status', ['urgent', 'high'])
dependsOnNot(string $field, $value)
Show container when field does NOT equal value:
NovaDependencyContainer::make([ Text::make('Cancellation Reason'), ])->dependsOnNot('status', 'active')
dependsOnNotIn(string $field, array $values)
Show container when field value is NOT in array:
NovaDependencyContainer::make([ Text::make('Additional Info'), ])->dependsOnNotIn('status', ['completed', 'cancelled'])
dependsOnEmpty(string $field)
Show container when field is empty:
NovaDependencyContainer::make([ Text::make('Default Value'), ])->dependsOnEmpty('custom_value')
dependsOnNotEmpty(string $field)
Show container when field is NOT empty:
NovaDependencyContainer::make([ Textarea::make('Description'), ])->dependsOnNotEmpty('title')
dependsOnNullOrZero(string $field)
Show container when field is null or zero:
NovaDependencyContainer::make([ Text::make('Free tier features'), ])->dependsOnNullOrZero('subscription_plan')
Chaining Multiple Dependencies
You can chain multiple dependencies together. ALL conditions must be met:
NovaDependencyContainer::make([ Text::make('Premium Features'), Text::make('Custom Domain'), ])->dependsOn('plan', 'premium') ->dependsOnNotEmpty('company_name') ->dependsOnNot('status', 'suspended')
Using with Closures
You can pass a closure to dynamically generate fields:
NovaDependencyContainer::make(function () { return [ Text::make('Dynamic Field 1'), Text::make('Dynamic Field 2'), ]; })->dependsOn('type', 'dynamic')
Apply to Fields (Flat Structure)
Use applyToFields() to apply dependencies without the container wrapper:
NovaDependencyContainer::make([ Text::make('Field 1'), Text::make('Field 2'), ])->dependsOn('type', 'special') ->applyToFields()
Adding Dependencies to Regular Fields
You can also add dependencies directly to regular Nova fields using the HasDependencies trait:
use Iamgerwin\NovaDependencyContainer\HasDependencies; use Laravel\Nova\Fields\Text; class CustomTextField extends Text { use HasDependencies; } // In your Nova resource: CustomTextField::make('Special Field') ->dependsOn('type', 'custom')
Using with Flexible Fields
NovaDependencyContainer works seamlessly with whitecube/nova-flexible-content Flexible fields:
use Iamgerwin\NovaDependencyContainer\NovaDependencyContainer; use Whitecube\NovaFlexibleContent\Flexible; use Laravel\Nova\Fields\Select; use Laravel\Nova\Fields\Text; Flexible::make('Overlay Items') ->addLayout('Overlay Item', 'overlay_item', [ Select::make('Type') ->options([ 'Default' => 'Default', 'Location' => 'Location', 'Contact Us' => 'Contact Us', ]), NovaDependencyContainer::make([ Text::make('Recipient Email', 'recipient_email') ->rules('nullable', 'email', 'max:255'), ])->dependsOn('type', 'Contact Us'), NovaDependencyContainer::make([ Text::make('Location Name', 'location_name'), Text::make('Address', 'address'), ])->dependsOn('type', 'Location'), ]),
The package automatically detects the Flexible field context and resolves field attributes correctly, even with dynamically prefixed attribute names.
For more details, see the Flexible Field Support documentation.
Advanced Examples
Multi-Step Form
public function fields(NovaRequest $request) { return [ Select::make('Step') ->options([ '1' => 'Basic Info', '2' => 'Address', '3' => 'Confirmation', ]), NovaDependencyContainer::make([ Text::make('First Name')->required(), Text::make('Last Name')->required(), Text::make('Email')->required(), ])->dependsOn('step', '1'), NovaDependencyContainer::make([ Text::make('Street Address')->required(), Text::make('City')->required(), Text::make('Zip Code')->required(), ])->dependsOn('step', '2'), NovaDependencyContainer::make([ Boolean::make('Confirm Details'), Textarea::make('Additional Notes'), ])->dependsOn('step', '3'), ]; }
Conditional Validation
public function fields(NovaRequest $request) { return [ Select::make('Payment Method') ->options([ 'credit_card' => 'Credit Card', 'bank_transfer' => 'Bank Transfer', 'paypal' => 'PayPal', ]), NovaDependencyContainer::make([ Text::make('Card Number') ->required() ->rules('required', 'credit_card'), Text::make('CVV') ->required() ->rules('required', 'digits:3'), ])->dependsOn('payment_method', 'credit_card'), NovaDependencyContainer::make([ Text::make('Bank Account') ->required(), Text::make('Routing Number') ->required(), ])->dependsOn('payment_method', 'bank_transfer'), NovaDependencyContainer::make([ Text::make('PayPal Email') ->required() ->rules('required', 'email'), ])->dependsOn('payment_method', 'paypal'), ]; }
Testing
Run the test suite:
composer test
Run tests with coverage:
composer test-coverage
Code Quality
Format code with Laravel Pint:
composer format
Run static analysis with PHPStan:
composer analyse
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
This package is inspired by alexwenzel/nova-dependency-container.
License
The MIT License (MIT). Please see License File for more information.
iamgerwin/nova-dependency-container 适用场景与选型建议
iamgerwin/nova-dependency-container 是一款 基于 Vue 开发的 Composer 扩展包,目前已累计 1.05k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「container」 「dependency」 「nova」 「laravel」 「field」 「conditional」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 iamgerwin/nova-dependency-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iamgerwin/nova-dependency-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 iamgerwin/nova-dependency-container 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova card that shows you your system information.
A Laravel Nova card.
A Laravel Nova package for publishable fields
A Laravel Nova package for translatable fields
A fast and intuitive dependency injection container.
A Laravel Nova Image field. you can paste or drag or chose an image
统计信息
- 总下载量: 1.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-25