Session Doesn't Work on Redirect

Updated 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