Session Doesn't Work on Redirect

Created at 08-Mar-2022 , By samar

Trying to display the error and success message in blade file after redirect using session in Laravel. In my case Session doesn't wok on redirect.

  • //Import session facade
    use Illuminate\Support\Facades\Session;
    
    //Controller's method
    if(!$condition){
        Session::flash('message', 'Error message !');
        Session::flash('alert-class', 'alert-danger');
        return redirect()->back();
    }
    Session::flash('message', 'Success message !');
    Session::flash('alert-class', 'alert-success');
    return redirect()->back();
    
    //Blade view 
    @if(Session::has('message'))
    <div class="alert {{ Session::get('alert-class', 'alert-info') }} alert-dismissible fade show">
        {{ Session::get('message') }}
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    @endif
    

Back to code snippet queries related laravel

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.