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???
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
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.