How to Use SweetAlert2 with AJAX in Laravel 8.x & Up

Published on October 14, 2020 2 min read

We're available to do freelance project. Take a look at our services!

In this article, I am going to share how to use SweetAlert2 in Laravel 8.x and Upper with AJAX. Let’s start.

Table of Contents

  1. Create Controller
  2. Define Routes & Insert Fake data
  3. Set SweetAlert2 with AJAX in Blade File
  4. Run and See Output
  5. Tested With

Create Controller

Create user controller using this command:

php artisan make:controller UserController

Open user controller and paste this code:


namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class UserController extends Controller
    // all users
    public function users()
        $users = User::all();
        return view('users', compact('users'));

    // delete user
    public function delete($id)
        $delete = User::destroy($id);

        // check data deleted or not
        if ($delete == 1) {
            $success = true;
            $message = "User deleted successfully";
        } else {
            $success = true;
            $message = "User not found";

        //  return response
        return response()->json([
            'success' => $success,
            'message' => $message,

I’ve created two functions. One for getting all users and another for deleting user.

Define Routes & Insert Fake data

Let’s define routes for those two functions:


use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;

Route::get('users', [UserController::class, 'users']);
Route::post('delete/{id}', [UserController::class, 'delete']);

Now let’s insert some fake user data:

\App\Models\User::factory(5)->create(); // 5 fake user data

Set SweetAlert2 with AJAX in Blade File

Create a view named “users.blade.php” and paste this code:

<!doctype html>
<html lang="en">
    <!-- meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <!-- csrf-token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="">
    <!-- jquery -->
    <script src=""></script>
    <!-- SweetAlert2 -->
    <link rel="stylesheet" href="">
    <script src=""></script>

    <title>How to Use SweetAlert2 with AJAX in Laravel 8.x</title>
<body class="container" style="margin-top: 40px;">

<div class="row" style="margin-bottom: 20px;">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">

<table class="table table-bordered">
        <th width="280px">Actions</th>
    @foreach ($users as $user)
            <td>{{ $user->id }}</td>
            <td>{{ $user->name }}</td>
            <td>{{ $user->email }}</td>
                <button class="btn btn-danger" onclick="deleteConfirmation({{$user->id}})">Delete</button>

<script type="text/javascript">
    function deleteConfirmation(id) {{
            title: "Delete?",
            icon: 'question',
            text: "Please ensure and then confirm!",
            type: "warning",
            showCancelButton: !0,
            confirmButtonText: "Yes, delete it!",
            cancelButtonText: "No, cancel!",
            reverseButtons: !0
        }).then(function (e) {

            if (e.value === true) {
                var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

                    type: 'POST',
                    url: "{{url('/delete')}}/" + id,
                    data: {_token: CSRF_TOKEN},
                    dataType: 'JSON',
                    success: function (results) {
                        if (results.success === true) {
                  "Done!", results.message, "success");
                            // refresh page after 2 seconds
                        } else {
                  "Error!", results.message, "error");

            } else {

        }, function (dismiss) {
            return false;

Run and See Output

Now run the Laravel project and visit this route http://localhost:8000/users. You will see a page like this:

Click Delete button and the SweetAlert2 confirmation box will appear:

If you select Yes button, the data will be deleted via AJAX and you will see a confirmation message like this:

Tested With

During writing this article, I’ve used:

  • Laravel: v8.9.0
  • SweetAlert2: v10.5.1
  • Boostrap: v5.0.0-alpha2
  • jQuery: v3.5.1
That’s it. Thanks for reading. 🙂


Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.


Leave a Reply

Your email address will not be published. Required fields are marked *