underscoresdev
3 months ago

Undefined variable when passing data to a function or method

Posted 3 months ago by underscoresdev

Hey folks ๐Ÿ‘‹

I wonder if anyone can shed any light on this weird issue I'm having. I've got a Controller method store() which needs to send some data to a function which returns that data in a format I want; however no matter what I do, I keep getting an Undefined variable error - here's the code:

In the controller ($validInput comes from validating the incoming request)

$fieldConfig = create_form_config( $validInput );

The function:

function create_form_config( array $inputย ) {

    // Optional fields
    $optionalFields = ['recipient', 'save', 'webhook'];

    // Setup required fields
    $fieldConfig = [
        'name'      => $input['name'],
        'fields'    => $input['fields']
    ];

    // Add optional fields
    foreach ( $optionalFields as $key => $value ) {
        if ( !is_null($value) ) $fieldConfig[$key] = $value; 
    }

    return $fieldConfig;

}

The supposed undefined variable is $input - the parameter for the function, which makes 0 sense to me. If I dd( func_get_args() ) I can see the data is in there. What am I missing? ๐Ÿ˜•

Any help is much appreciated, this is driving me nuts!

  • Chris

Please sign in or create an account to participate in this conversation.