Session Doesn't Work on Redirect

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.

Answers 1
  • //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
    
    1
  • Back to code snippet queries related laravel