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

PrinceMinky's avatar

Pagination, Modals and Livewire

Hello, I have run into an issue.

I have looped modals for each user that is displayed like so:

    @foreach ($this->users as $user)
    <flux:modal name="edit-user-{{ $user->id }}" class="w-full space-y-6">
        @include('livewire.admin.user-management.form')
    </flux:modal>

    <flux:modal name="delete-profile-{{ $user->id }}" class="min-w-[22rem] space-y-6">
        <div>
            <flux:heading size="lg">Delete User?</flux:heading>
            <flux:subheading>
                <p>You're about to delete the user: {{ $user->name }}.</p>
                <p>This action cannot be reversed.</p>
            </flux:subheading>
        </div>
        <div class="flex gap-2">
            <flux:spacer />
            <flux:modal.close>
                <flux:button variant="ghost">Cancel</flux:button>
            </flux:modal.close>
            <flux:button type="submit" variant="danger" wire:click="delete({{ $user->id }})">Delete User</flux:button>
        </div>
    </flux:modal>
    @endforeach

My issue comes when I click onto another page. Once I load onto another page I am unable to open a model to edit/confirm a delete.

I'm sure it has something to do with the rendering of a volt component and the need to add a listener, but I am unsure.

I'll post my code below (documented for ease)

0 likes
4 replies
jj15's avatar
jj15
Best Answer
Level 10

I'm not too familiar with Flux, but since you're rendering the components in a loop, you might need to add the wire:key attribute so that Livewire can keep track of them.

On an extra note, is it necessary to have a modal for each user? I've always accomplished this using a single modal component that accepts the ID of the model it needs to populate the details for. I then have the "Edit" button dispatch an event that changes this ID.

1 like
PrinceMinky's avatar

@jj15 Hello, the reason I did that was because there was a flicker when opening the modal and the data binding. I eventually got a work around and used the class to open the modal instead.

I appreciate all inputs from all.

As for the issue with the pagination, I got that working!

    /**
     * Show the user form modal for adding or editing a user
     * 
     * @param User|null $user The user to edit. If null, shows add form
     * @return void
     */
    public function showUserForm(?User $user = null): void
    {
        $this->resetValidation();
        
        if ($user) {
            $this->userId = $user->id;
            $this->name = $user->name;
            $this->email = $user->email;
            $this->username = $user->username;
            $this->selectedRoles = $user->getRoleNames();
        } else {
            $this->resetFormFields();
        }
        
        Flux::modal('user-form')->show();
    }
1 like
jj15's avatar

@Minky I see. Glad you were able to get it working!

Snapey's avatar

wire:key, and agree with @jj15 , a modal per user is overkill

Please or to participate in this conversation.