When looping through data in a Livewire template using
@foreach, you must add a uniquewire:keyattribute to the root element rendered by the loop.
https://livewire.laravel.com/docs/components#adding-wirekey-to-foreach-loops
I'm just starting using Livewire 3, I have done this many times in Livewire 2 and works, so this puzzles me.
Loginc in my component is quite easy: send the paginated User to the blade:
public function render()
{
return view('livewire.user.index', [
'users' => \App\Models\User::where('name', 'like', "%$this->search%")
->paginate(10),
]);
}
Then in the view, inside the foreach I have something as simple as:
@foreach ($users as $user)
<button wire:click="destroy({{ $user->id }})">Destroy {{ $user->name }}</button>
@endforeach
The destroy method is as simple as:
public function destroy(\App\Models\User $user) {
$user->delete();
}
So, problem here is, when I click the button the first time, it actually deletes the model, I can check it in the database, it also removes the button, but the following cllicks in the other buttons does nothing. If I inspect the view, of course I get the wire.click=destroy(28) or whatever id
I thought it had something to do with the pagination, so I took it out, and the problem persists.
Is there something new in Livewire 3? I'm not using Livewire components, so I think I don't have to use the :key property... or do I?
When looping through data in a Livewire template using
@foreach, you must add a uniquewire:keyattribute to the root element rendered by the loop.
https://livewire.laravel.com/docs/components#adding-wirekey-to-foreach-loops
Please or to participate in this conversation.