PHPackages                             angrymoustache/nova-dependency-container - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. angrymoustache/nova-dependency-container

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

angrymoustache/nova-dependency-container
========================================

A Laravel Nova field container allowing to depend on other fields values

04PHP

Since Dec 15Pushed 4mo agoCompare

[ Source](https://github.com/AngryMoustache/nova-dependency-container)[ Packagist](https://packagist.org/packages/angrymoustache/nova-dependency-container)[ RSS](/packages/angrymoustache-nova-dependency-container/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Nova Field Dependency Container
===============================

[](#nova-field-dependency-container)

[![Latest Version on Packagist](https://camo.githubusercontent.com/d80841b1db5f458ed1b28b11e7b52facfdd9bc68f8da92c2f19f3e55f4c57069/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f65706172746d656e742f6e6f76612d646570656e64656e63792d636f6e7461696e65722e737667)](https://packagist.org/packages/epartment/nova-dependency-container)[![Total Downloads](https://camo.githubusercontent.com/937b4f2942108c47509b9a52deec17294d66b245c4bee4a8a188ee311709844e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f65706172746d656e742f6e6f76612d646570656e64656e63792d636f6e7461696e65722e737667)](https://packagist.org/packages/epartment/nova-dependency-container)[![License](https://camo.githubusercontent.com/f508e7cf9e9d46c47b830bf644cb9f36db1d46bab8fb8b9e3616aaa47be7ce7f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f65706172746d656e742f6e6f76612d646570656e64656e63792d636f6e7461696e65722e737667)](https://github.com/epartment/nova-dependency-container/blob/master/LICENSE.md)

### Description

[](#description)

A container for grouping fields that depend on other field values. Dependencies can be set on any field type or value.

### Demo

[](#demo)

[![Demo](https://raw.githubusercontent.com/epartment/nova-dependency-container/master/docs/demo.gif)](https://raw.githubusercontent.com/epartment/nova-dependency-container/master/docs/demo.gif)

### Versions

[](#versions)

- install v1.2.x for Laravel v5.8 or v6.x and Nova 2.x
- install v1.1.2 for Laravel v5.7 and Nova v1.x

### Installation

[](#installation)

The package can be installed through Composer.

```
composer require epartment/nova-dependency-container
```

### Usage

[](#usage)

1. Add the `Epartment\NovaDependencyContainer\HasDependencies` trait to your Nova Resource.
2. Add the `Epartment\NovaDependencyContainer\NovaDependencyContainer` to your Nova Resource `fields` method.
3. Add the `Epartment\NovaDependencyContainer\ActionHasDependencies` trait to your Nova Actions that you wish to use dependencies on.

```
class Page extends Resource
{
    use HasDependencies;

    public function fields(Request $request)
    {
        return [

            Select::make('Name format', 'name_format')->options([
                0 => 'First Name',
                1 => 'First Name / Last Name',
                2 => 'Full Name'
            ])->displayUsingLabels(),

            NovaDependencyContainer::make([
                Text::make('First Name', 'first_name')
            ])->dependsOn('name_format', 0),

        ];
    }
}
```

### Dependencies

[](#dependencies)

The package supports four kinds of dependencies:

1. `->dependsOn('field', 'value')`
2. `->dependsOnIn('field', ['value', 'value2'])`
3. `->dependsOnNot('field', 'value')`
4. `->dependsOnNotIn('field', ['value', 'value2'])`
5. `->dependsOnEmpty('field')`
6. `->dependsOnNotEmpty('field')`
7. `->dependsOnNullOrZero('field')`

These dependencies can be combined by chaining the methods on the `NovaDependencyContainer`:

```
NovaDependencyContainer::make([
  // dependency fields
])
->dependsOn('field1', 'value1')
->dependsOnNotEmpty('field2')
->dependsOn('field3', 'value3')
```

The fields used as dependencies can be of any Laravel Nova field type. Currently only two relation field types are supported, `BelongsTo` and `MorphTo`.

Here is an example using a checkbox:

[![Demo](https://raw.githubusercontent.com/epartment/nova-dependency-container/master/docs/demo-2.gif)](https://raw.githubusercontent.com/epartment/nova-dependency-container/master/docs/demo-2.gif)

### BelongsTo dependency

[](#belongsto-dependency)

If we follow the example of a *Post model belongsTo a User model*, taken from Novas documentation [BelongsTo](https://nova.laravel.com/docs/2.0/resources/relationships.html#belongsto), the dependency setup has the following construction.

We use the singular form of the `belongsTo` resource in lower case, in this example `Post` becomes `post`. Then we define in dot notation, the property of the resource we want to depend on. In this example we just use the `id` property, as in `post.id`.

```
BelongsTo::make('Post'),

NovaDependencyContainer::make([
    Boolean::make('Visible')
])
->dependsOn('post.id', 2)
```

When the `Post` resource with `id` 2 is being selected, a `Boolean` field will appear.

### BelongsToMany dependency

[](#belongstomany-dependency)

A [BelongsToMany](https://nova.laravel.com/docs/2.0/resources/relationships.html#belongstomany) setup is similar to that of a [BelongsTo](https://nova.laravel.com/docs/2.0/resources/relationships.html#belongsto).

The `dependsOn` method should be pointing to the name of the intermediate table. If it is called `role_user`, the setup should be

```
BelongsToMany::make('Roles')
	->fields(function() {
		return [
			NovaDependencyContainer::make([
			    // pivot field rules_all
			    Boolean::make('Rules All', 'rules_all')
			])
			->dependsOn('role_user', 1)
		]
	}),
```

If the pivot field name occurs multiple times, consider using [custom intermediate table models](https://laravel.com/docs/6.x/eloquent-relationships#defining-custom-intermediate-table-models) and define it in the appropiate model relation methods. The only reliable solution I found was using mutators to get/set a field which was being used multiple times. Although this may seem ugly, the events which should be fired on the intermediate model instance, when using an Observer, would work unreliable with every new release of Nova.

> If Nova becomes reliable firing eloquent events on the intermediate table, I will update this examples with a more elegant approach using events instead.

Here is an (ugly) example of a get/set mutator setup for an intermediate table using a pivot field called `type`.

```
// model User
class User ... {

   public function roles() {
   		return $this->belongsToMany->using(RoleUser::class)->withPivot('rules_all');
   }

}

// model Role
class Role ... {

   public function users() {
   		return $this->belongsToMany->using(RoleUser::class)->withPivot('rules_all');
   }

}

// intermediate table
use Illuminate\Database\Eloquent\Relations\Pivot;
class RoleUser extends Pivot {

	protected $table 'role_user';

	public function getType1Attribute() {
	    return $this->type;
	}

	public function setType1Attribute($value) {
		$this->attributes['type'] = $value;
	}

	// ... repeat for as many types as needed
}
```

And now for the dependency container.

```
->fields(function() {
	return [
		NovaDependencyContainer::make([
		    // pivot field rules_all
		    Select::make('Type', 'type_1')
		    	->options([
		    		/* some options */
	    		])
		    	->displayUsingLabels()
		])
		->dependsOn('role_user', 1)
		,

		NovaDependencyContainer::make([
		    // pivot field rules_all
		    Select::make('Type', 'type_2')
		    	->options([
		    		/* different options */
	    		])
		    	->displayUsingLabels()
		])
		->dependsOn('role_user', 2)
		,

		// .. and so on
	]
}),
```

### MorphTo dependency

[](#morphto-dependency)

A similar example taken from Novas documentation for [MorphTo](https://nova.laravel.com/docs/2.0/resources/relationships.html#morphto) is called commentable. It uses 3 Models; `Comment`, `Video` and `Post`. Here `Comment` has the morphable fields `commentable_id` and `commentable_type`

For a `MorphTo` dependency, the following construction is needed.

`Commentable` becomes lower case `commentable` and the value to depend on is the resource singular form. In this example the dependency container will add two additional fields, `Additional Text` and `Visible`, only when the `Post` resource is selected.

```
MorphTo::make('Commentable')->types([
    Post::class,
    Video::class,
]),

NovaDependencyContainer::make([
    Text::make('Additional Text', 'additional'),
    Boolean::make('Visible', 'visible')
])
->dependsOn('commentable', 'Post')
```

### License

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/epartment/nova-dependency-container/blob/master/LICENSE.md) for more information.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance53

Moderate activity, may be stable

Popularity3

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity12

Early-stage or recently created project

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/a0823716f39cd253642c2b62390b95420214aa6a18e0038d194a7853ee5dc154?d=identicon)[AngryMoustache](/maintainers/AngryMoustache)

---

Top Contributors

[![wize-wiz](https://avatars.githubusercontent.com/u/9093559?v=4)](https://github.com/wize-wiz "wize-wiz (36 commits)")[![ragingdave](https://avatars.githubusercontent.com/u/1168344?v=4)](https://github.com/ragingdave "ragingdave (21 commits)")[![michielkempen](https://avatars.githubusercontent.com/u/14795113?v=4)](https://github.com/michielkempen "michielkempen (12 commits)")[![mikaelpopowicz](https://avatars.githubusercontent.com/u/5689944?v=4)](https://github.com/mikaelpopowicz "mikaelpopowicz (6 commits)")[![milewski](https://avatars.githubusercontent.com/u/2874967?v=4)](https://github.com/milewski "milewski (6 commits)")[![daanadriaan](https://avatars.githubusercontent.com/u/29752116?v=4)](https://github.com/daanadriaan "daanadriaan (3 commits)")[![tufankilicaslan](https://avatars.githubusercontent.com/u/701360?v=4)](https://github.com/tufankilicaslan "tufankilicaslan (3 commits)")[![almeidafranci](https://avatars.githubusercontent.com/u/9145755?v=4)](https://github.com/almeidafranci "almeidafranci (2 commits)")[![TheoKouzelis](https://avatars.githubusercontent.com/u/4980126?v=4)](https://github.com/TheoKouzelis "TheoKouzelis (2 commits)")[![dododedodonl](https://avatars.githubusercontent.com/u/100052?v=4)](https://github.com/dododedodonl "dododedodonl (1 commits)")[![sash](https://avatars.githubusercontent.com/u/45210?v=4)](https://github.com/sash "sash (1 commits)")[![StanAngeloff](https://avatars.githubusercontent.com/u/93127?v=4)](https://github.com/StanAngeloff "StanAngeloff (1 commits)")[![yaroslawww](https://avatars.githubusercontent.com/u/23663794?v=4)](https://github.com/yaroslawww "yaroslawww (1 commits)")[![bennofication](https://avatars.githubusercontent.com/u/7251115?v=4)](https://github.com/bennofication "bennofication (1 commits)")[![vkazakevich](https://avatars.githubusercontent.com/u/9676524?v=4)](https://github.com/vkazakevich "vkazakevich (1 commits)")[![wesdeboer](https://avatars.githubusercontent.com/u/901355?v=4)](https://github.com/wesdeboer "wesdeboer (1 commits)")[![AngryMoustache](https://avatars.githubusercontent.com/u/20287748?v=4)](https://github.com/AngryMoustache "AngryMoustache (1 commits)")[![niektenhoopen](https://avatars.githubusercontent.com/u/3450011?v=4)](https://github.com/niektenhoopen "niektenhoopen (1 commits)")

### Embed Badge

![Health badge](/badges/angrymoustache-nova-dependency-container/health.svg)

```
[![Health](https://phpackages.com/badges/angrymoustache-nova-dependency-container/health.svg)](https://phpackages.com/packages/angrymoustache-nova-dependency-container)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
