0

I have a file called all.blade.php which goes like this:

@component('admin.layouts.content' , ['title' => 'example file'])
   @slot('breadcrumb')
        <li class="breadcrumb-item active">Example File</li>
   @endslot
   ...
@endcomponent

@push('scripts')
    <script>
        Swal.fire({
            title: 'Do you want to save the changes?',
            showDenyButton: true,
            showCancelButton: true,
            confirmButtonText: 'Save',
            denyButtonText: `Don't save`,
        }).then((result) => {
            /* Read more about isConfirmed, isDenied below */
            if (result.isConfirmed) {
                Swal.fire('Saved!', '', 'success')
            } else if (result.isDenied) {
                Swal.fire('Changes are not saved', '', 'info')
            }
        })
    </script>
@endpush

And here is content.blade.php:

@extends('admin.master')

@section('content')
   {{ $slot }}
@endsection

And this is master.blade.php:

  <!DOCTYPE html>
  <html>
     <head>
        ...
        @stack('scripts')
     </head>
     <body>
        @yield('content')
     </body>
   </html>

Now the problem is @push('scripts') ... @endpush does not work out and not showing the sweet alert message.

So what's going wrong here? How can I solve this issue and call the @component and @push together at a blade?

japose7523
  • 29
  • 2
  • 15

2 Answers2

1

Swap the order of @push and @component

@push('scripts')
    <script>
        Swal.fire({
            title: 'Do you want to save the changes?',
            showDenyButton: true,
            showCancelButton: true,
            confirmButtonText: 'Save',
            denyButtonText: 'Don\'t save',
        }).then((result) => {
            /* Read more about isConfirmed, isDenied below */
            if (result.isConfirmed) {
                Swal.fire('Saved!', '', 'success')
            } else if (result.isDenied) {
                Swal.fire('Changes are not saved', '', 'info')
            }
        })
    </script>
@endpush

@component('admin.layouts.content' , ['title' => 'example file'])
   @slot('breadcrumb')
        <li class="breadcrumb-item active">Example File</li>
   @endslot
   ...
@endcomponent

Another option would be to convert the master into component

<!-- resources/views/components/admin/master.blade.php -->
<!DOCTYPE html>
  <html>
     <head>
        ...
        {{ $scripts ?? '' }}
     </head>
     <body>
        {{ $slot }}
     </body>
   </html>

And convert content into a component

<!-- resources/views/components/admin/layouts/content.blade.php -->
<x-admin.master>
    <x-slot:scripts>
       {{ $scripts ?? '' }}
    </x-slot>
    {{ $slot }}

   <!-- Any other slots etc -->
</x-admin.master>

Then you can write the all.blade.php as

<x-admin.layouts.content>
    <x-slot:scripts>
        <script>
            Swal.fire({
                title: 'Do you want to save the changes?',
                showDenyButton: true,
                showCancelButton: true,
                confirmButtonText: 'Save',
                denyButtonText: 'Don\'t save',
            }).then((result) => {
                /* Read more about isConfirmed, isDenied below */
                if (result.isConfirmed) {
                    Swal.fire('Saved!', '', 'success')
                } else if (result.isDenied) {
                    Swal.fire('Changes are not saved', '', 'info')
                }
            })
        </script>
    </x-slot>
    <h1>All</h1>
</x-admin.layouts.content>
Donkarnash
  • 12,433
  • 5
  • 26
  • 37
0

Try Ctrl+U to check if that component is rendered to its position. Also mention, If you are using Swal a JS library so did the actual Swallibrary code load before calling the fire() function.

Last please check the browser console option to identify if it was some JS error, not the blade.

Hafijul Islam
  • 24
  • 1
  • 5
  • If I cut and paste the js codes into head section of `master.blade.php` it will work out fine but now it does not load it at all! There's a problem with the way I'm calling the `@push` – japose7523 Jun 19 '22 at 09:03
  • In fact I don't want to load this js code in every file, just in the `all.blade.php` file – japose7523 Jun 19 '22 at 09:47