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

vincent15000's avatar

Error : $selectedAuthors[1] not found in the component

Hello,

I have this problem with an array.

I need to display checkboxes with authors from the database.

In my component I declare public $selectedAuthors = [];.

What I need in my view is this.

{{ $selectedAuthors[$role->id] }}

And $selectedAuthors[$role->id] is an array which contains the list of the ids of the selectedAuthors.

Livewire display an error saying that $selectedAuthors[1] is not found in the component.

I add here my view for the checkboxes.

<div class="col-4">
    <ul class="list-group">
        @foreach ($roles as $role)
            <li class="list-group-item">
                <div class="d-flex flex-row justify-content-between">
                    <div>{{ $role->name }} : {{ json_encode($selectedAuthors[$role->id]) }}</div>
                    <div><i class="fas fa-arrow-alt-circle-down" cursor="pointer" data-bs-toggle="collapse" data-bs-target="#role-{{ $role->id }}-content"></i></div>
                </div>
            </li>
            <li class="list-group-item collapse" id="role-{{ $role->id }}-content">
                @foreach ($authors as $author)
                    <div wire:key="role.{{ $role->id }}.author.{{ $author->id }}" class="form-check mb-1">
                        <input wire:model="selectedAuthors.{{ $role->id }}" class="form-check-input" type="checkbox" id="role.{{ $role->id }}.author.{{ $author->id }}">
                        <label class="form-check-label" for="role.{{ $role->id }}.author.{{ $author->id }}">{{ $author->fullname }}</label>
                    </div>
                @endforeach
            </li>
        @endforeach
    </ul>
</div>

Do you have any idea how to solve this ?

Thanks a lot ;).

Vincent

0 likes
9 replies
tykus's avatar

If you dump $selectedAuthors in the view, what do you get?

@dump($selectedAuthors)

I don't see why you should expect the $role->id index to exist in the array???

vincent15000's avatar

@tykus I get this.

array:2 [▼
  1 => []
  2 => []
]

And if I select only one author, all authors are selected and the dump shows this.

array:2 [▼
  1 => array:1 [▼
    0 => "on"
  ]
  2 => []
]

While I'd like to have an array of the selected author ids instead of a simple on.

Snapey's avatar

what if you have more roles than selected authors?

vincent15000's avatar

@Snapey For example an author role is : author, illustrator, co-author, ... I'm not sure to understand your question about the number of the roles ... would it be a problem if I had more roles than authors ? But this won't be the case. For the moment I have 2 roles and 3 authors to test.

Snapey's avatar

i can't honestly tell what you are trying to do

vincent15000's avatar

@Snapey I have a form to save a new game. A game has one or many authors, one or many illstrators, ... I manage the roles (author, illustrator, ...) in the database.

In the form view, I have a list with all the existing roles and for each role I display a list (with collapse) with all authors with checkboxes.

If I click on the author role, it displays the list of authors. If I click on the illsutrator role, it displays the list of authors (distinct from the precedent list for the authors).

vincent15000's avatar

I just tried with a select with multiple selection and I get exactly what I want ... except I want to do this with checkboxes.

array:2 [▼
  1 => array:2 [▼
    0 => "14"
    1 => "2"
  ]
  2 => []
]
hmreumann's avatar
Level 6

Why don't you instantiate the roles in the $selectedAuthors array in the mount method?

public function mount(){
	foreach ($this->roles as $role){
		$this->selectedAuthors[$role->id] = [];
	}
}

Please or to participate in this conversation.