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

MyirLik's avatar

Swal not defined laravel

error  'Swal' is not defined  no-undef

import swal from 'sweetalert2';

window.Swal = swal;

Is defined but doesn`t work

alertDelete() {
        Swal.fire({
            title: 'You can`t delete this',
            text: "You can`t do this",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'Yes!',
          })
      },
0 likes
7 replies
saurav77's avatar

@kikogyh add the CDN links in the head section

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.9/sweetalert2.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/6.6.9/sweetalert2.min.js"></script>
@section('scripts')
    @if (alert()->ready())
        <script>
            swal({
                title: "{!! alert()->message() !!}"
            });
        </script>
    @endif
@endsection

but the specification is PHP Version: 7.1.9 Try this

vandan's avatar

@kikogyh

import swal from 'sweetalert2';

window.swal = swal

or

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@8"></script>
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Where is this function?

alertDelete() {
        Swal.fire({
            title: 'You can`t delete this',
            text: "You can`t do this",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'Yes!',
          })
      },

Make sure it is run after your app.js file where you attach Swal to window

MyirLik's avatar

i so I solved it, I had to use window.swal.fire in vue file

blackforestcode's avatar

I installed it today to my Project. I had to use the "new" keyword, because it creates a new instance of Swal.

new Swal({...}), with it it's working for me.

1 like

Please or to participate in this conversation.