sweet-alert: Uncaught ReferenceError: swal is not defined

Hi, Rashid

I had to wrap the alert in a section: (in this case ‘scripts’)

@section('scripts')
    @if (alert()->ready())
        <script>
            swal({
                title: "{!! alert()->message() !!}"
            });
        </script>
    @endif
@endsection

And then add:

@yield('scripts')

below my other script files in the layout template to make it work. Otherwise a get an Uncaught ReferenceError: swal is not defined error.

Can you tell me why or is there another workaround to do this?

Thank you for this package!

Richard

Specifications:

  • PHP Version: 7.1.9
  • Laravel Version: 5.6

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 16 (8 by maintainers)

Most upvoted comments

Hi,

You just add the CDN links in the head section like below

<head>
  <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>
</head>

add the yield after the content yield like below

@yield('content')
{{-- Here --}}
@yield('scripts')

now you can use this with any view that extended the layout

@section('scripts')
    @if (alert()->ready())
        <script>
            swal({
                title: "{!! alert()->message() !!}",
                type: "{!! alert()->type() !!}",
                @if (alert()->option('timer'))
                    timer: {!! alert()->option('timer') !!},
                    showConfirmButton: false
                @endif
            });
        </script>
    @endif
@endsection

note: in my case the layout file is layout/app.blade.php

Thank you for using the package!