1

I wonder in my application can't show flash message. I have tried many solutions on stackoverflow but my problem is not solved.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;

class DebugController extends Controller
{ 

public function get()
    {
        // Visit direct page flash session is working
        // Eg: localhost/debug/get
        // But if I send request all flash sessions are not working
        return Redirect::route('home')->with('success', 'Working session on visit direct page!');
    }
    
public function post(Request $request)
    {    
        // All flash sessions are not working
        session()->flash('anything', 'Session not working!');

        // Working session
        session()->put('message', 'Working session!');

        // Session not working, is 'success' key reserved?
        session()->put('success', 'Session not working!');
        
        return Redirect::route('home')->with('anything', 'Session not working!'); // Session not working
    }
}

Route:

Route::get('debug/get', 'DebugController@get');
Route::post('debug/post', 'DebugController@post');

View:

@if(Session::has('success'))
    <div class="alert alert-success">
        {{ Session::get('success') }}
    </div>
@endif


// Working session
@if(Session::has('message'))
    <div class="alert alert-success">
        {{ Session::get('message') }}
    </div>
    {{ session()->forget('message') }}
@endif

I have tried to modify middle ware in Kernel from this solution but still not working

Laravel Version: 8.x.x
PHP Version: 7.4.x

1 Answers1

1

You will need to do Session::has('anything') in your view instead of Session::has('success').

The first parameter is the key that can be accessed with has or get.

If the above is already done, you might need to use the Facade: Session::flash('anything','content');

Take a look at this.


In case you want to remove the Session data you have to use flush instead of flash().

Laravel documentation