You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.6.3中session->flash()无效,无法回显错误消息求助

Troubleshooting Laravel Flash Message Not Showing Up

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.php to 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:
    Flash::error('Error message');
    return Redirect::back();
    
    (Or the shorter 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:00:13