delucaspinto

[LARAVEL 5.6] MESSAGE ALERT SWALFIRE

Jul 22nd, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.05 KB | None | 0 0
  1. //função JSs usando Sweetalert2
  2. function comsucesso(valor) {
  3.   const Toast = Swal.mixin({
  4.     toast: true,
  5.     position: 'top-end',
  6.     showConfirmButton: false,
  7.     timer: 3000
  8.   });
  9.  
  10.   Toast.fire({
  11.     type: 'success',
  12.     title: valor + ' com sucesso!'
  13.   })
  14. }
  15.  
  16. // No controller eu uso 'cadastrado' ou 'removido'
  17. public function deletarLancamentos($id){
  18.     $decrypted = Crypt::decryptString($id);
  19.     $lancamento = Lancamento::find($decrypted);
  20.     if ($lancamento) {
  21.         $lancamento->delete();
  22.         session()->flash('alert-success', 'removido');
  23.         return back();
  24.     } else {
  25.         return back();
  26.     }
  27. }
  28.  
  29. //No HTML(blade) eu uso essa validação
  30.  
  31. @if(session('alert-success') == 'cadastrado')
  32. <script>
  33.   comsucesso('Cadastrado');
  34. </script>
  35. <?php session()->forget('alert-success'); ?>
  36. @elseif(session('alert-success') == 'removido')
  37. <script>
  38.   comsucesso('Removido');
  39. </script>
  40. <?php session()->forget('alert-success'); ?>
  41. @endif
  42.  
  43. por fim eu uso um forget, pois se ele usar um refresh não irá mostrar o alerta novamente.
Advertisement
Add Comment
Please, Sign In to add comment