sfarzoso's avatar

array_key_exists(): Argument #2 ($array) must be of type array, Illuminate\View\ComponentAttributeBag given

I have created a component called Input and I'm calling it in the following way:

<x-input type="text" class="test" name="fullname" placeholder="hello" required></x-input>

The component use the following class:

`<?php

namespace App\View\Components;

use Illuminate\View\Component;

class Input extends Component { public $name; public $type; public $prepend; public $prependText; public $append; public $appendText;

/**
 * Create a new component instance.
 *
 * @return void
 */
public function __construct($name)
{
    $this->name = $this->dot_str($name);
}

/**
 * Get the view / contents that represent the component.
 *
 * @return \Illuminate\Contracts\View\View|\Closure|string
 */
public function render()
{
    return view('components.input');
}

private function dot_str($string)
{
    return str_replace(['[', ']'], ['.', ''], $string);
}

}`

and this is the blade file:

{!! Form::{$type ?? 'text'}($name, old($name, $value ?? ''), $attributes->merge(['class' => $errors->first($name, ' is-invalid')])) !!}

as you can see I'm using laravelcollective/html and when I load the page I get:

array_key_exists(): Argument #2 ($array) must be of type array, Illuminate\View\ComponentAttributeBag given (View: /usr/src/app/resources/views/components/input.blade.php)

0 likes
5 replies
sr57's avatar

$this->name = $this->dot_str($name);

$name is not an array but an object

dd this variable to see its content and find the attribute you want o get the name.

sfarzoso's avatar

@sr57 Thanks for the answer but the problem isn't related to the $name variable, the problem is related to this line:

{!! Form::{$type ?? 'text'}($name, old($name, $value ?? ''), array_merge(['class' => 'form-control'], $attributes)) !!}

the $attributes property isn't an array but a ComponentAttributeBag I don't understand how can I merge the dynamic attributes of the component with the default attributes setted in the view.

sfarzoso's avatar

@sr57 I'm confused, the author says the issues has been patched but I still see a ComponentAttributeBag, so what is the solution to this problem?

sr57's avatar

@sfarzoso

Did not read this for Issue 2, but a alternative with 5 up votes.

Have no app with this package, can't test by myself.

Please or to participate in this conversation.