Laravel 5.6.3中session->flash()无效,无法回显错误消息求助
Hey Rick, let’s walk through this issue step by step— I’ve dealt with this exact frustration before, so I know how annoying it is when flash messages refuse to show up. Let’s break down the most common fixes:
1. Verify You’re Using the Laracasts Flash Package Correctly
Since you’re using @include('flash::message'), you’re likely relying on the laracasts/flash package. Let’s confirm the setup is right:
- First, make sure you’ve installed the package via Composer:
composer require laracasts/flash - Next, check your
config/app.phpto ensure the service provider and alias are registered:// In 'providers' array Laracasts\Flash\FlashServiceProvider::class, // In 'aliases' array 'Flash' => Laracasts\Flash\Flash::class, - In your controller, use the package’s method instead of a generic
flash()call. For error messages, it should be:
(Or the shorterFlash::error('Error message'); return Redirect::back();return back();if you prefer.)
2. Confirm Web Middleware Includes Session Support
Flash messages depend on sessions, so your web middleware group must have these critical middlewares. Open app/Http/Kernel.php and check the web group:
protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, // Required \Illuminate\View\Middleware\ShareErrorsFromSession::class, // Required \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], ];
If either StartSession or ShareErrorsFromSession are missing, add them back—without them, session data (including flash messages) won’t persist between requests.
3. Check Your View Setup
- Make sure
@include('flash::message')is placed in a part of your template that actually renders (like your main layout file, inside the<body>tag). If it’s nested inside a conditional block that never triggers, it won’t show up. - The laracasts flash messages use Bootstrap styling by default. If you haven’t included Bootstrap CSS in your project, the message might be on the page but invisible. Add the Bootstrap stylesheet to your layout to fix this.
4. Debug the Session Directly
To narrow down if the issue is with storing the message or rendering it, add this temporary line to your view:
{{ dd(session()->all()) }}
Look for a key called flash_notification—this is what the laracasts package uses to store flash messages. If you see it, the problem is in your view rendering; if not, the message isn’t being saved to the session correctly (double-check your controller code and middleware).
5. Try the Native Laravel Flash Method (If Ditching the Package)
If you want to skip the laracasts package, use Laravel’s native flash functionality instead:
- In your controller:
return back()->with('error', 'Error message'); - In your view, replace
@include('flash::message')with:@if(session('error')) <div class="alert alert-danger"> {{ session('error') }} </div> @endif
内容的提问来源于stack exchange,提问作者Rick J

