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

mallorca's avatar

Confirm Delete using Sweetalert

I'm using laravel 5.1 and I'm new to the framework.

I have added sweetalert and managed to create a form which successfully deletes items. I would like to create a solution now that makes the user confirm before deleting using sweetalert.

I managed to make the modal pop up correctly but it deletes the article instantly and I need some help from a experienced artisan to make it so that the item doesnt get deleted unless I press "Yes, delete it!" in the sweet alert.

Controller:

    public function destroy($id)
    {
        flash()->delete('Warning!', 'Article successfully deleted!');
        $article = Article::findOrFail($id);
        $article->destroy($id);
        return redirect('/admin/articles');
    }

flash.php:

class Flash {

   public function create($title, $message, $level, $key = 'flash_message')
    {
      return session()->flash($key, [
        'title'    =>   $title,
        'message'  =>   $message,
        'level'     =>  $level
        ]);
    }

    public function delete($title, $message, $level = 'warning')
    {
      return $this->create($title, $message, $level, 'flash_message_delete');
    }

flash.blade.php (which is imported)

@if (session()->has('flash_message_delete'))

<script type="text/javascript">
  
  swal({   //removed the brackets from session because laracasts doesn't show them 
    title: "session('flash_message_delete.title')",   
    text: "session('flash_message_delete.message')",   
    type: "session('flash_message_delete.level')",   
    showCancelButton: true,   
    confirmButtonColor: "#DD6B55",   
    confirmButtonText: "Yes, delete it!",   
    cancelButtonText: "No, cancel please!",   
    closeOnConfirm: false,   
    closeOnCancel: false
    },

    function(isConfirm){   
      if (isConfirm) 
        {     
          swal("Deleted!", "Your file has been deleted.", "success");   
        } 

      else
        {     
          swal("Cancelled", "Your file is safe", "error");   
        }
      });

</script>

@endif

form view:

{!! Form::model($article, ['method' => 'delete', 'action' => ['ArticlesController@destroy', $article->id]]) !!}
    {{ csrf_field() }}
    
    {!! Form::button('<i class="fa fa-trash-o"></i>', ['class' => 'btn btn-danger', 'type'=>'submit']) !!}

    {!! Form::close() !!}

I would really appreciate it if someone could help me here. Thanks!

0 likes
3 replies
krenor's avatar

You could use a normal button instead of a submit button.
For that you could create a JQuery listener like

$('button.fa-trash-o').on('click', function(){
  // Display SweetAlert
  // On clicking 'Yes, I am sure to delete'
  if( SweetalertConfirmed() )
    {
      $(this).closest('form').submit();
    }
})
1 like
mallorca's avatar

@Krenor Thanks for you reply! I couldn't make this to work.. Could you please give a more detailed example?

Please or to participate in this conversation.