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

Demers94's avatar

Session not persisting for alerts

Hello everyone,

I'm trying to add a proper flash messaging system with SweetAlert, I strongly inspired myself of the episode of ProjectFlyer.

Here's how I defined my custom function :

function flash($message, $title = 'Info', $type = 'info')
{   
    session()->flash('flash_message', [
        'message' => $message,
        'title' => $title,
        'type' => $type,        
    ]); 
}

This is in a helpers.php file that I'm autoloading.

Here's how I'm using it :

[...]
$post->save();

    flash('The post has been selected as the answer', 'Success', 'success');
    return back();

Finally, here's how I'm checking that it's set, in the app template that I extend in each of my views :

@if(session()->has('flash_message'))
    <script>
        swal({
            title: '{{ session("flash_message.title") }}',
            text : '{{ session("flash_message.message") }}',
            type : '{{ session("flash_message.type") }}',
            timer: 1500,
            showConfirmButton: false,           
        })
    </script>
@endif

There aren't any errors in the console. It works fine if I call the flash() function directly in a test route, like this :

Route::get('/test', function(){
    flash('Test', 'Title', 'success');
    return view('welcome');
});

... but it does not work if I call it from my controller before redirecting. Could it have something to do with the fact that I'm calling the back() method directly after setting the flash message?

Thanks for the help !

0 likes
5 replies
mikebarwick's avatar

Going to go out on a limb here and say that the swal() function likely needs to be wrapped in a document ready function. Add console.log('test') to see that the method is actually being called.

This should come after the jquery script has been loaded. In my views, I typically add a @section('footer') // content @end and put all my js in there. Then in my master file, below my master js scripts (jquery, etc.), I'll render the footer section there. I.e. @yield('footer')

<script>
$(function() {
    swal({
            title: '{{ session("flash_message.title") }}',
            text : '{{ session("flash_message.message") }}',
            type : '{{ session("flash_message.type") }}',
            timer: 1500,
            showConfirmButton: false,           
        });
});
</script>
Demers94's avatar

@mikebarwick Thanks for the reply!

I tried wrapping it in a document ready function but it still does not work. I tried a console.log, but it doesn't show up, indicating that it doesn't even enter the @if statement.

If I do a var_dump of session()->has('flash_message'); on the page, I get false. If I do that same var_dump right after I set the session in my custom flash() function, I get true, so it's being unset before the code has a chance to run. :(

Demers94's avatar

Update

I tried it in another method of my controller (the show() one that displays a topic) and it works, so the problem seems to be with the redirection that occurs when I select/de-select a post as the answer.

I tired removing the back() and manually going back to the previous page, but it's not working, so I'm really lost here.

Demers94's avatar

@anzze That worked! I noticed the new web middleware after it upgrades to 5.2, but I didn't think anything of it.

Please or to participate in this conversation.