Search code examples
laravelflash-message

Laracasts/flash not showing in laravel 5.4


I have recently upgraded my project from Laravel 5.1 to 5.4. Everything is working fine except for flash messages which are not displaying. I have perform the following checks to make sure I am not doing something wrong:

  1. RouteServiceProvider.php

The web middleware group is added automatically to my routes by mapWebRoutes function

+--------+----------+-----+--------+------------------------------------------+--------------+
| Domain | Method   | URI | Name   | Action                                   | Middleware   |
+--------+----------+-----+---------------------------------------------------+--------------+
|        | GET|HEAD | /   |        | App\Http\Controllers\HomeController@index| web,auth     |
  1. Kernel.php

Below is the content of the middleware groups in Kernel.php

 'web' => [
                \App\Http\Middleware\EncryptCookies::class,
                \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
                \Illuminate\Session\Middleware\StartSession::class,
                // \Illuminate\Session\Middleware\AuthenticateSession::class,
                \Illuminate\View\Middleware\ShareErrorsFromSession::class,
                \App\Http\Middleware\VerifyCsrfToken::class,
                \Illuminate\Routing\Middleware\SubstituteBindings::class,
            ],
  1. flash() is placed before redirects

    flash()->success('Bob stone Added Successfully'); $target_location = 'cia_agent/' . $agent_id .'/profile'; return redirect($target_location);

Just one 302 reponse is returned in Network in Chrome developer tool.

The above code works in Laravel 5.1, what are my missing here?


Solution

  • After digging, I found the solution. In /resources/views/vendor/flash/message.blade.php, I had the following code which was working for Laravel 5.1

    @if (Session::has('flash_notification.message'))
        @if (Session::has('flash_notification.overlay'))
            @include('flash::modal', ['modalClass' => 'flash-modal', 'title' => Session::get('flash_notification.title'), 'body' => Session::get('flash_notification.message')])
        @else
            <div class="alert alert-{{ Session::get('flash_notification.level') }}">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
    
                {{ Session::get('flash_notification.message') }}
            </div>
        @endif
    @endif
    

    But since there is been a change with Laracasts/flash view partial used to display messages, I put the new code in the file:

    @foreach ((array) session('flash_notification') as $message)
        @if ($message['overlay'])
            @include('flash::modal', [
                'modalClass' => 'flash-modal',
                'title'      => $message['title'],
                'body'       => $message['message']
            ])
        @else
            <div class="alert
                        alert-{{ $message['level'] }}
                        {{ $message['important'] ? 'alert-important' : '' }}"
                 role="alert"
                    >
                @if ($message['important'])
                    <button type="button"
                            class="close"
                            data-dismiss="alert"
                            aria-hidden="true"
                            >&times;</button>
                @endif
    
                {!! $message['message'] !!}
            </div>
        @endif
    @endforeach
    
    {{ session()->forget('flash_notification') }}
    

    It's working perfectly now.