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.
- 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
- Nova 4 & 5 Compatible: Works with Laravel Nova 4.x and 5.x (tested with 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
- PHP 8.3 or higher
- Laravel 11.x or 12.x
- Laravel Nova 4.x or 5.x
You can install the package via composer:
composer require iamgerwin/nova-dependency-container
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'),
];
}
Show container when field equals specific value:
NovaDependencyContainer::make([
Text::make('Company Name'),
])->dependsOn('type', 'business')
Show container when field value is in array:
NovaDependencyContainer::make([
Text::make('Priority Note'),
])->dependsOnIn('status', ['urgent', 'high'])
Show container when field does NOT equal value:
NovaDependencyContainer::make([
Text::make('Cancellation Reason'),
])->dependsOnNot('status', 'active')
Show container when field value is NOT in array:
NovaDependencyContainer::make([
Text::make('Additional Info'),
])->dependsOnNotIn('status', ['completed', 'cancelled'])
Show container when field is empty:
NovaDependencyContainer::make([
Text::make('Default Value'),
])->dependsOnEmpty('custom_value')
Show container when field is NOT empty:
NovaDependencyContainer::make([
Textarea::make('Description'),
])->dependsOnNotEmpty('title')
Show container when field is null or zero:
NovaDependencyContainer::make([
Text::make('Free tier features'),
])->dependsOnNullOrZero('subscription_plan')
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')
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')
Use applyToFields()
to apply dependencies without the container wrapper:
NovaDependencyContainer::make([
Text::make('Field 1'),
Text::make('Field 2'),
])->dependsOn('type', 'special')
->applyToFields()
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')
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'),
];
}
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'),
];
}
Run the test suite:
composer test
Run tests with coverage:
composer test-coverage
Format code with Laravel Pint:
composer format
Run static analysis with PHPStan:
composer analyse
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
This package is inspired by alexwenzel/nova-dependency-container.
The MIT License (MIT). Please see License File for more information.