-1

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"

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')
                ];
    }

Do you know how to solve the issue? Thanks!

Ozzdev
  • 65
  • 1
  • 11

1 Answers1

1

Not sure what is does, but you are sending an array to a class that class has a method toArray().

that's why i always use return types in my code cause it will break if the input does not match.

Could it be that it thinks that this is a collection or a class that needs to be inputted.

in some cases i am lazy and will try the following:

collect(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')
                ],)

because many packages uses Collection as a way to handle, filter and search an array.

but this is more a guess than actuall knowledge. Look at the function getUpdateRules() in the vendor map, what does it do.

/**
     * @param Resource|Model $resource
     *
     * @return array
     */
    public function resolveDependencyFieldUsingResource($resource): array
    {

        $matched = $this->runConditions(
            $this->flattenRelationships($resource)
        );

        return $matched ? $this->fields->toArray() : [];

    }

this is the function so as you can see it expects a model or resource class

according to the docs:

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

your return array

array_merge(
            [ // <--- note the extra bracket

                [
                    Select::make('Option', 'option')
                  ->options([
                      1 => 'Option 1',
                      2 => 'Option 2',
                      3 => 'Option 3',
                  ]),
                  ConditionalContainer::make([ Text::make('Field A') ])->if('option = 1')
                ],
    
Marcel Santing
  • 103
  • 1
  • 5
  • Thanks, but it shows a syntax error like that. – Ozzdev Feb 24 '22 at 15:18
  • getUpdateRules() that function is not in the package, did you create this? – Marcel Santing Feb 24 '22 at 15:24
  • Here it 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 its in the packge right? – Ozzdev Feb 24 '22 at 15:26
  • I edited my awnser – Marcel Santing Feb 24 '22 at 15:32
  • so look at your output of the array merge I think you have to look there for the awnser – Marcel Santing Feb 24 '22 at 15:37
  • Thanks, like that it shows a different error "message: "array_merge(): Argument #2 must be of type array, Laravel\\Nova\\Fields\\Text given" ". – Ozzdev Feb 24 '22 at 15:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/242372/discussion-between-ozzdev-and-marcel-santing). – Ozzdev Feb 24 '22 at 15:49