Delete method with Sweet Alert in Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Efficiently Handling Delete Methods Using Sweet Alert in Laravel Applications
Introduction: Sweet Alert is a powerful library that enhances the usability of JavaScript alerts by providing customizable, responsive, and visually appealing messages. In this blog post, we will explore how to effectively integrate Sweet Alert with Laravel for a better user experience when deleting crucial data from your application.
1 - Setting up the Environment: To use Sweet Alert in your Laravel project, first download the sweetalert.css and sweetalert.min.js files from their respective locations. Next, include these files in your app.blade.php file as shown below:
<!-- Sweet Alert -->
<link href="{{ asset('/dist/css/sweetalert.css') }}" rel="stylesheet">
<!-- Sweet Alert -->
<script src="{{ asset('/dist/js/sweetalert.min.js') }}"></script>
2 - Create the Delete Button with Sweet Alert: In your Laravel application, you can use the JavaScript onclick event to create a button that triggers the Sweet Alert function for confirmation before deleting data. Here's an example code snippet:
{!! Form::open(['method' => 'DELETE','route' => ['users.destroy', $user->id],'style'=>'display:inline']) !!}
<button onclick="
swal({
title: 'Esta seguro de realizar esta Acción?',
text: 'Si procede este usuario será eliminado!',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Eliminar!',
cancelButtonText: 'Cancelar',
closeOnConfirm: false,
closeOnCancel: false
},
function(){
swal('Usuario eliminado!', 'Este usuario fue eliminado de nuestros registros.', 'success');
});"
class="btn btn-danger" data-toggle="tooltip" data-placement="top" title="Eliminar usuario"> <i class="material-icons">delete</i>
</button>
{!! Form::close() !!}
3 - Implementing the Delete Method: Lastly, you need to create a dedicated method in your UserController for handling the deletion of users. Here's an example implementation:
public function destroy($id)
{
User::find($id)->delete();
return redirect()->route('users.index')
->with('success','User deleted successfully');
}
4 - Troubleshooting the Issue: Unfortunately, when deleting a user in this scenario, the Sweet Alert message displays but closes automatically without allowing for confirmation actions. This can occur due to an issue with the auto-submitting form on click of the confirm button within Sweet Alert. To solve this problem, you need to modify the JavaScript code as follows:
swal({
title: 'Esta seguro de realizar esta Acción?',
text: 'Si procede este usuario será eliminado!',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Eliminar!',
cancelButtonText: 'Cancelar',
closeOnConfirm: false,
closeOnCancel: false
},
function(){
if (confirm) {
swal('Usuario eliminado!', 'Este usuario fue eliminado de nuestros registros.', 'success');
// Call the delete method after successful confirmation
User::find($user_id)->delete();
return redirect()->route('users.index')
->with('success','User deleted successfully');
} else {
// Do nothing or perform alternative actions if user cancels the action
}
});
Conclusion: Integrating Sweet Alert with Laravel is a powerful way to increase the usability and efficiency of your application while providing users with a more engaging experience. By following these steps, you will have created an effective method for handling delete methods in your Laravel project. If you encounter any issues or have additional questions, don't hesitate to reach out for help from experts within the Laravel community.