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

ctrlaltdelme's avatar

Render many Livewire components at once?

Is the proper way to render many Livewire components at once to just loop over them and add a wire:key?

The problem is this I'm using it to populate the DOM with some modals for editing their corresponding item from a table and I feel like that just is messy and generates too much overhead. It probably doesn't but just wondering if there's a better way to do this.

Thanks!

0 likes
3 replies
AlgeRiany01's avatar

Yes, you can definitely render multiple Livewire components inside a loop using @foreach and wire:key, and it works fine But if u are using it for modals or edit forms for each item in a list, it might feel a bit messy and heavy and you are right to think about performance and organization.

There are actually two clean approaches you can take:

@foreach ($items as $item)
    <button wire:click="$dispatch('edit-Item', {{ $item->id }})">Edit</button>
@endforeach

<livewire:edit-item-modal /> 

In the EditItemModal component:

public $item;


#[On('edit-Item')]
public function editItem($id)
{
    $this->item = Item::find($id);
}


or u can make full livewire page for editing so u need make route and get id to get product for edit like so :

Route::get('/items/{id}/edit', EditItemPage::class)->name('items.edit');

Then in your Livewire EditItemPage component:

public $item;

public function mount($id)
{
    $this->item = Item::findOrFail($id);
}

u can build it however you want

2 likes
ctrlaltdelme's avatar

@AlgeRiany01 Thanks for the response. I tried the first method and I can't actually get the modal to open. I'm using Flux that comes with the Laravel starter kits. I have the trigger wrapped around the "Edit" button (which are in a loop like in your example), and I have the Livewire Modal component outside the loop. But I can't actually get it to open. Is there anything specific I need to call or make sure of, like the name or anything?

parwina's avatar

@ctrlaltdelme, Hi, see bellow example;

///

<flux:modal name="category-modal" class="md:w-96">
    <div class="space-y-6">
        <div>
            <flux:heading size="lg">Category Information</flux:heading>
            {{-- <flux:text class="mt-2">Category details.</flux:text> --}}
        </div>
        <flux:input wire:model='name' label="Category name" placeholder="Category name" />

        <flux:radio.group wire:model="is_countable" label="Is this category countable?">
            <flux:radio value="1" label="Yes" checked />
            <flux:radio value="0" label="No" />

        </flux:radio.group>

        <div class="flex">
            <flux:spacer />
            @if ($update)
                <flux:button wire:click='updateCat' icon='pencil-square' size='sm' type="submit" variant="danger">Save Changes
                </flux:button>
            @else
                <flux:button wire:click='save' icon='plus-circle' size='sm' type="submit" variant="primary">Add category
                </flux:button>
            @endif

        </div>
    </div>
</flux:modal>
{{-- @if (session('success'))
    <div class="p-3 bg-green-700 text-white rounded text-center">{{ session('success') }}</div>
@endif --}}

@if (session('success'))
    <flux:callout variant="success" icon="check-circle" heading=" {{ session('success') }}"/> 
@endif
<table class="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
    <thead class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
        <tr>
            <th scope="col" class="px-3 py-2">
                ID
            </th>
            <th scope="col" class="px-3 py-2">
                Name
            </th>
            <th scope="col" class="px-3 py-2">
                is_countable ?
            </th>
            <th scope="col" class="px-3 py-2" colspan="2">
                Actions
            </th>
        </tr>
    </thead>
    <tbody>

        @forelse ($categories as $category)
        <tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700 border-gray-200">
            <td class="px-3 py-2">{{ $category->id }}</td>
            <td class="px-3 py-2">{{ $category->name }}</td>
            <td class="px-3 py-2">{{ $category->is_countable }}</td>
            <td><flux:button icon='pencil-square' size='sm' variant='primary' wire:click='edit({{ $category->id }})'>Edit</flux:button> </td>
            <td><flux:button  icon='minus-circle' size='sm' wire:click="delete({{ $category->id }})" wire:confirm='Are you sure deleting {{ $category->name }}?'
                variant="danger">Delete</flux:button></td>
        </tr>
        @empty
            <tr>
                <th colspan="4" class="px-3 py-2" align="center">No category found !</th>
            </tr>
        @endforelse
    </tbody>
</table>

Please or to participate in this conversation.