Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

OzzDev's avatar

Change the method to allow pass different values as parameters for the same variable

I have a method like below and it's working I have a $fields variable that then I need to merge it to another array.

public function definition(): array
    {

           $fields  = 
            NovaDependencyContainer::make([

                $this->listDefinition(
                    ‘Field’,
                    $this->getFields(),
                    2 // here can be 2 or 4
                )])
            ->dependsOn(‘is’_active, 0)
            ->meta()['fields'][0];

           return array_merge(
            [
                …
            ],
            $fields
        );
    }

However I want to have some way of having 2 variables $fields. Basically have something like below, which of course as it is doesn't work:

public function definition(): array
        {
    
               $fields  = 
                NovaDependencyContainer::make([
    
                    $this->listDefinition(
                        ‘Field’,
                        $this->getFields(),
                        2 // here can be 2 or 4
                    )])
                ->dependsOn(‘is’_active, 0)
                ->meta()['fields'][0];


               $fields  = 
                NovaDependencyContainer::make([
    
                    $this->listDefinition(
                        ‘Field’,
                        $this->getFields(),
                        4
                    )])
                ->dependsOn(‘is’_active, 1)
                ->meta()['fields'][0];

               return array_merge(
                [
                    …
                ],
                $fields
            );
        }

The only difference is the 2 or 4, that's the number of times the field will appear. And the other difference is the "dependsOn(‘is’_active, 1)" it can be 1 or 0 "dependsOn('is_checked', 0)".

I'm using a package "Epartment\NovaDependencyContainer" and basically if the checkbox "Is Checked" is selected I want to have "4", because with "4" it will show the fields 4 times otherwise I want to have 2.

Do you know how to properly change the method to allow for that?

0 likes
4 replies
Nakov's avatar

Have you tried this:

$fields  = 
                NovaDependencyContainer::make([
    
                    $this->listDefinition(
                        ‘Field’,
                        $this->getFields(),
                        2 // here can be 2 or 4
                    )])
                ->dependsOn(‘is’_active, 0)
                ->meta()['fields'][0];


               $fields2  = 
                NovaDependencyContainer::make([
    
                    $this->listDefinition(
                        ‘Field’,
                        $this->getFields(),
                        4
                    )])
                ->dependsOn(‘is’_active, 1)
                ->meta()['fields'][0];

               return array_merge(
                [
                    …
                ],
                $fields,
				$fields2,
            );

you can add many arrays to the merge.

1 like
OzzDev's avatar

@Nakov @Nakov Thanks, like that it's doesn't show any error but when the page is loaded at first shows like below and if I check the checkbox remains the same:

Field 1

					...fields regarding this field section

Field 2 ...fields regarding this field section

Field 1

					...fields regarding this field section

Field 2

					...fields regarding this field section

Field 3

					...fields regarding this field section

Field 4

					...fields regarding this field section
OzzDev's avatar

@Nakov Yeah, sorry, I understnad I'm using Nova and I didn't exlplain well maybe with an image was easier but it seems that is not possible to upload image.

But I can explain in other way, for example like below, using only one of the $field arrays on the merge, this works properly, it shows 2 times the fields:

$fields = NovaDependencyContainer::make([

                $this->listDefinition(
                    ‘Field’,
                    $this->getFields(),
                    2 // here can be 2 or 4
                )])
            ->dependsOn(‘is’_active, 0)
            ->meta()['fields'][0];


           $fields2  = 
            NovaDependencyContainer::make([

                $this->listDefinition(
                    ‘Field’,
                    $this->getFields(),
                    4
                )])
            ->dependsOn(‘is’_active, 1)
            ->meta()['fields'][0];

           return array_merge(
            [
                …
            ],
            $fields,
        );

And like below, using only one of the $field arrays on the merge, also works properly, it shows 4 times the fields:

$fields = NovaDependencyContainer::make([

                $this->listDefinition(
                    ‘Field’,
                    $this->getFields(),
                    2 // here can be 2 or 4
                )])
            ->dependsOn(‘is’_active, 0)
            ->meta()['fields'][0];


           $fields2  = 
            NovaDependencyContainer::make([

                $this->listDefinition(
                    ‘Field’,
                    $this->getFields(),
                    4
                )])
            ->dependsOn(‘is’_active, 1)
            ->meta()['fields'][0];

           return array_merge(
            [
                …
            ],
            $fields2,
        );

So the solution seems that cannot be like this because with the 2 arrays on the merge the fields dont show properly.

Please or to participate in this conversation.