PHP code example of epartment / nova-dependency-container

1. Go to this page and download the library: Download epartment/nova-dependency-container library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

epartment / nova-dependency-container example snippets


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),

        ];
    }
}

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

BelongsTo::make('Post'),

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

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

// 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
}

->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::make('Commentable')->types([
    Post::class,
    Video::class,
]),

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