Toastr use Laravel session variables as warning messages

I want to change from Bootstrap

alert to Toastr

alert. My current working setup:

@if (Session::has('flash_notification.message'))

     <div class="alert alert-{{ Session::get('flash_notification.level') }}">
         <button type="button" class="close" data-dismiss="alert" 
             aria-hidden="true">&times;</button>
         {{ Session::get('flash_notification.message') }}
     </div>

 @endif

      

But I am currently struggling to access Laravel session variables in JS.

Actual working JS example:

$(document).ready(function() {

    toastr.info('Page Loaded!');

    });

});

      

But I want to use Laravel session variables to include different messages and alert boxes:

@if (Session::has('flash_notification.message'))

    <script>
        $(document).ready(function() {

            toastr.options.timeOut = 4000;
            toastr.{{ Session::get('flash_notification.level') }}('{{ Session::get('flash_notification.message) }}');

        });
    </script>

@endif

      

I am getting various errors like unexpected ;

. Any help would be greatly appreciated. Many thanks.

+3


source to share


1 answer


Since you are working in a blade template, you can output the contents of a variable / function with {{ var/function() }}

. If you want to get unescaped you can use {!! var/function() !!}

.

So, the solution to your problem is that you need to surround your php code with tags {{ }}

:



@if (Session::has('flash_notification.message'))

    <script>
        $(document).ready(function() {

        toastr.{{ Session::get('flash_notification.level') }}
        ('{{ Session::get('flash_notification.message') }}');

        });
    </script>

@endif

      

+2


source







All Articles