Skip to content
Advertisement

Laravel 7 does not display flash message

Laravel Framework 7.25.0
PHP 7.2 on LARAGON

I’m not sure if it has anything to do with it, but I believe it may be something related to my routes, I may be talking nonsense.

No flash message is being displayed, I was only able to display a message by setting it to Session::put, retrieving it in the view and then deleting it.

Session::put('success', 'example');
return redirect()->route('example', array('id_example' => 'example'));

When I use with, withErrors, flash on the front always returns null.

return redirect()->route('example', array('id_example' => 'example'))->with('success', 'Text');
or
return redirect()->back()->withErrors( ['example'] );
or
Session::flash('success', 'example');

In the view I’ve tried several ways, all of them have the same result. EXCEPT when I use Session::put manually to store the return message.

@if (Session::has('error'))
@if ($errors->any())
@if (Session::has('success'))
@if (session()->has('success'))

This is my model that I use on routes.

Route::namespace('Front')->group(function(){
    # Homepage
     Route::get('/', 'ExampleFrontController@Home')->name('home');
})

Route::namespace('Admin')->group(function(){
    # Login
     Route::get('/admin/login', 'ExampleAdminController@Index')->name('admin.login');
})

I have no problem using Session :: put, however, the returns from my Form Requests Rules are also not displayed and this is causing me a problem.

Advertisement

Answer

After asking the question, I decided to take a test that I had not yet done.

I commented on the kernel the class: StartSession::class and ShareErrorsFromSession::class.

As planned, the sessions stopped working.

In my case, I had to move both classes from $ middlewareGroups to $ middleware, this solved my problem above.

I changed that:

    protected $middleware = [
        // AppHttpMiddlewareTrustHosts::class,
        AppHttpMiddlewareTrustProxies::class,
        FruitcakeCorsHandleCors::class,
        AppHttpMiddlewareCheckForMaintenanceMode::class,
        IlluminateFoundationHttpMiddlewareValidatePostSize::class,
        AppHttpMiddlewareTrimStrings::class,
        IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,
    ];

    protected $middlewareGroups = [
        'web' => [
            AppHttpMiddlewareEncryptCookies::class,
            IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
            IlluminateSessionMiddlewareStartSession::class,
            // IlluminateSessionMiddlewareAuthenticateSession::class,
            IlluminateViewMiddlewareShareErrorsFromSession::class,
            AppHttpMiddlewareVerifyCsrfToken::class,
            IlluminateRoutingMiddlewareSubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            IlluminateRoutingMiddlewareSubstituteBindings::class,
        ],
    ];

for this

    protected $middleware = [
        // AppHttpMiddlewareTrustHosts::class,
        IlluminateSessionMiddlewareStartSession::class,
        IlluminateViewMiddlewareShareErrorsFromSession::class,
        AppHttpMiddlewareTrustProxies::class,
        FruitcakeCorsHandleCors::class,
        AppHttpMiddlewareCheckForMaintenanceMode::class,
        IlluminateFoundationHttpMiddlewareValidatePostSize::class,
        AppHttpMiddlewareTrimStrings::class,
        IlluminateFoundationHttpMiddlewareConvertEmptyStringsToNull::class,
    ];

    protected $middlewareGroups = [
        'web' => [
            AppHttpMiddlewareEncryptCookies::class,
            IlluminateCookieMiddlewareAddQueuedCookiesToResponse::class,
        #    IlluminateSessionMiddlewareStartSession::class,
        #    IlluminateViewMiddlewareShareErrorsFromSession::class,
        #    IlluminateSessionMiddlewareAuthenticateSession::class,
            AppHttpMiddlewareVerifyCsrfToken::class,
            IlluminateRoutingMiddlewareSubstituteBindings::class,
        ],
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement