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

kowu1's avatar
Level 2

Edit modal state without server-side state management Livewire

Hi, Im developing an app using Livewire with Laravel Breeze and have implemented a modal provided by Breeze. It works well for adding a user, but I'm trying to figure out if it's possible to manage the state of an edit modal without server-side state management.

I know that Jetstream uses a different approach where the modal is managed by server-side Livewire, and I've tried using it for edits, but it isn't as smooth as the one based on Alpine.js with dispatch, which takes over 300ms to respond.

Is there a way to use the Breeze modal based on Alpine.js and dispatch, and only make a server request to fetch the user data when a user is clicked? Or would it be better to use Axios in this case? Any help would be appreciated.

0 likes
1 reply
LaryAI's avatar
Level 58

To manage the state of an edit modal in a Laravel Breeze application using Livewire without relying heavily on server-side state management, you can indeed leverage Alpine.js for the modal interactions and Livewire for handling data operations. This approach allows you to use the responsiveness of Alpine.js for UI updates and Livewire for server interactions when necessary.

Here's a step-by-step solution to implement this:

1. Setup the Livewire Component

First, create a Livewire component that will handle the user data fetching and updating.

php artisan make:livewire UserEdit

2. Livewire Component (UserEdit.php)

In your Livewire component, add methods to load and save user data.

namespace App\Http\Livewire;

use App\Models\User;
use Livewire\Component;

class UserEdit extends Component
{
    public $userId;
    public $name;
    public $email;

    public function mount($userId)
    {
        $this->loadUser($userId);
    }

    public function loadUser($userId)
    {
        $user = User::find($userId);
        $this->userId = $user->id;
        $this->name = $user->name;
        $this->email = $user->email;
    }

    public function save()
    {
        $user = User::find($this->userId);
        $user->update([
            'name' => $this->name,
            'email' => $this->email,
        ]);

        $this->emit('userUpdated');
    }

    public function render()
    {
        return view('livewire.user-edit');
    }
}

3. Livewire View (user-edit.blade.php)

Create the modal using Alpine.js for the frontend interactions.

<div x-data="{ open: false }">
    <button @click="open = true">Edit User</button>

    <div x-show="open" @click.away="open = false">
        <input type="text" wire:model="name">
        <input type="email" wire:model="email">
        <button wire:click="save">Save Changes</button>
        <button @click="open = false">Close</button>
    </div>
</div>

4. Triggering the Modal with Data

You can trigger the modal from another component or even a blade file by emitting an event to load the user and opening the modal.

<!-- Somewhere in your application -->
<button @click="$dispatch('load-user', { userId: 1 })">Edit User 1</button>

<script>
document.addEventListener('alpine:init', () => {
    Alpine.data('userEdit', () => ({
        open: false,
        init() {
            this.$root.addEventListener('load-user', event => {
                @this.call('loadUser', event.detail.userId)
                this.open = true;
            });
        }
    }));
});
</script>

5. Listening to Events

Ensure your Livewire component listens to the dispatched events and reacts accordingly.

This setup uses Alpine.js for handling the modal interactions (open/close) and Livewire for data fetching and updating, providing a smooth user experience while keeping the server interactions efficient.

Please or to participate in this conversation.