jantinnerezo / livewire-alert

SweetAlert2 wrapper for Livewire
https://livewire-alert.jantinnerezo.com
MIT License
682 stars 73 forks source link

delete user #140

Open Juanjosexdd opened 6 months ago

Juanjosexdd commented 6 months ago

I want to be able to delete a user's record, the alert works but the record is not being deleted when confirming, help.

view in component:

<button wire:click="destroy({{ $user->id }})">
      Eliminar
</button>
    public function destroy($userId)
    {
        $this->confirm('¿Estás seguro de que deseas eliminar este usuario?', [
            'showConfirmButton' => true,
            'confirmButtonText' => 'Si, Eliminar',
            'onConfirmed' => function() use ($userId) {
                $this->deleteUser($userId);
            }
        ]);
    }

    public function deleteUser($userId)
    {
        $user = User::find($this->userId);
        $user->delete();
        $this->alert('success', 'Usuario eliminado con éxito');
    }
Kotzilla commented 5 months ago

Change the code to this

public $userId;
protected $listeners = ['deleteUser'];

public function destroy($userId)
{
    $this->userId = $userId;
    $this->confirm('¿Estás seguro de que deseas eliminar este usuario?', [
        'showConfirmButton' => true,
        'confirmButtonText' => 'Si, Eliminar',
        'onConfirmed' => 'deleteUser'           
    ]);
}

public function deleteUser()
{
    $user = User::find($this->userId);
    $user->delete();
    $this->alert('success', 'Usuario eliminado con éxito');
}
Juanjosexdd commented 5 months ago

Ya lo hice y nada..

Kotzilla commented 5 months ago

I was update code. Can you try again ?

Juanjosexdd commented 5 months ago

no brother it doesn't work, I already tried it that way

szajens commented 4 months ago
use Livewire\Attributes\On;

class MenuList extends Component
{

  use LivewireAlert;

  public function destroy($userId)
  {
      $this->confirm('?Estás seguro de que deseas eliminar este usuario?', [
          'showConfirmButton' => true,
          'confirmButtonText' => 'Si, Eliminar',
          'onConfirmed' => 'confirmed',
          'data' => ['idToDelete' => $userId],
      ]);
  }

  #[On('confirmed')]
  public function deleteUser($data)
  {
      $user = User::find($data['idToDelete']);
      $user->delete();
      $this->alert('success', 'Usuario eliminado con éxito');
  }

}