Support the ongoing development of Laravel.io →
posted 1 year ago
Last updated by @ciskosv 1 year ago.
0

It seems that the issue you're facing is related to handling the modal closing and displaying the success message. You can modify your Livewire component and view to achieve the desired behavior by using emit and emitTo methods, and updating your view to listen for these events.

First, update your Livewire component to emit an event when the user is saved:

public function guardar()
{
    // ...validation and user creation code

    $successMessage = 'El nuevo usuario se ha creado exitosamente.';
    session()->flash('success', $successMessage);
    $this->emit('userSaved', ['success' => $successMessage]);
}

Next, update your view to listen for the userSaved event and close the modal when it's received. Also, update the script that listens for the guardado event to show the success message:

<!-- Add this script to your view -->
<script>
    window.addEventListener('livewire:load', function () {
        Livewire.on('userSaved', function (data) {
            $("#createDataModal").modal("hide");
            // Show the success message
            let successDiv = document.getElementById('success-message');
            successDiv.innerHTML = data.success;
            successDiv.style.display = 'block';
        });
    });
</script>

Finally, update your view to display the success message:

<!-- Add this div to your view to display the success message -->
<div id="success-message" class="alert alert-success" role="alert" style="display: none;"></div>

By following these steps, you should be able to close the modal when the user is saved and display the success message properly.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Stefan Rivera ciskosv Joined 21 Mar 2023

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.