Search code examples
laravelbootstrap-4eloquentbootstrap-modallaravel-livewire

Why the modal doesn't close in laravel livewire?


I'am using laravel livewire to delete records in two tables, the problem is the modal, the records are being deleted but the modal still shows.

the strange thing is that when I comment one of the lines of code to delete data, it works!

I'm using Bootstrap 4.1

this is my function:

    public function delete($id)
    {
        DB::beginTransaction();
        try 
        { 
            // If I comment on any of the following two lines (it doesn't matter what line it is), it works! 

            DetalleRamComputadora::where('fk_computadora', '=', $id)->delete();
            Computadora::where('id', '=', $id)->delete();

            DB::commit();
            $this->emit('confirm'); // Close modal "confirm"
            session()->flash('success', 'Registro eliminado con éxito');

        } catch (\Throwable $th) {
            DB::rollBack();
            $this->emit('confirm'); // Close modal "confirm"
            session()->flash('error', 'Ocurrió un error y no se almacenó el registro');
        }
    }

this is the script to close modal from livewire:

window.livewire.on('confirm', () => {
     $('#delete_confirm').modal('hide');
}); 

help me please!!


Solution

  • I was able to solve the problem. Only I added this code in the div of the modal

    **wire:ignore.self**
    
    <div wire:ignore.self class="modal fade" id="delete_confirm" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        ...
    </div>