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

OzzDev's avatar

Call to a member function toArray() on array

I’m trying to use this package: https://packagist.org/packages/digital-creative/conditional-container. However with this basic example:

    return array_merge(
        [

            Select::make('Option', 'option')
              ->options([
                  1 => 'Option 1',
                  2 => 'Option 2',
                  3 => 'Option 3',
              ]),

        /**
         * Only show field Text::make('Field A') if the value of option is equals 1
         */
        ConditionalContainer::make([ Text::make('Field A') ])->if('option = 1'),
        Text::make('Title'),
        Textarea::make('Description'),
        ],
        $arr
    );

I’m getting the error message: Call to a member function toArray() on array.

If I change to:

return array_merge(
            [

                [
                    Select::make('Option', 'option')
                  ->options([
                      1 => 'Option 1',
                      2 => 'Option 2',
                      3 => 'Option 3',
                  ]),
                  ConditionalContainer::make([ Text::make('Field A') ])->if('option = 1')
                ],

It shows:

message: "Call to a member function getUpdateRules() on array"

Do you know how to solve the issue? Thanks!

0 likes
4 replies
OzzDev's avatar

Even with only the example like below it shows "message: "Call to a member function toArray() on array":

 public function definition(): array
    {
        return [
                    Select::make('Option', 'option')
                  ->options([
                      1 => 'Option 1',
                      2 => 'Option 2',
                      3 => 'Option 3',
                  ]),
                  
                  ConditionalContainer::make([ Text::make('Field A') ])->if('option = 1')
                ];
    }
MohamedTammam's avatar

@OzzDev What are you doing with the returned value? It seems that there's a part of your app uses toArray() under the hood.

Try to convert the returned value to a collection.

public function definition(): array
    {
        return collect([
                    Select::make('Option', 'option')
                  ->options([
                      1 => 'Option 1',
                      2 => 'Option 2',
                      3 => 'Option 3',
                  ]),
                  
                  ConditionalContainer::make([ Text::make('Field A') ])->if('option = 1')
                ]);
    }
1 like
OzzDev's avatar

@MohamedTammam Thanks. The error shows like that "{message: "Call to a member function toArray() on array", exception: "Error",…} exception: "Error" file: "/var/www/vendor/digital-creative/conditional-container/src/‌​ConditionalContainer‌​.php" line: 354 message: "Call to a member function toArray() on array". So it seems that toArray() is used in the package right?

OzzDev's avatar

And I can't do like that because the method should return an array:

public function definition(): array { return [ Select::make('Option', 'option') ->options([ 1 => 'Option 1', 2 => 'Option 2', 3 => 'Option 3', ]),

              ConditionalContainer::make([ Text::make('Field A') ])->if('option = 1')
            ];
}

Please or to participate in this conversation.