Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

CKilgore87's avatar

Issue passing a Flash to a template

I am trying to pass something through to a Reset Password page when we have an issue with a third party API (Entra ID) where it can't set a password, and regardless of whatever I try to do, nothing is working.

The current approach I use is to pass something using Session::flash and then I redirect to the same page as before with the following code

} if ($entraResponse == 200) {
  // Not important
} else { 
  // We got a 4xx
            \Log::channel("entra")->error("There was an entra error in setUserPassword either creating or updating the user " . $user->email . " in Entra.");
            Session::flash("flash","Entra Error");
            return Redirect::back()->send();
}

And this should be picked up in the template, in this case its resources/views/auth/passwords/reset.blade.php

                    @if (Session::has('flash'))
                    <div class="alert alert-danger" role="alert">
                        There was an Error setting your password. Please contact support.
                        {{ print_r(Session::get('flash'), 1); }}
                    </div>
                    @endif

And regardless of whatever I do, I never get the session value to display. It never renders the Session display, even though the print_r does return 1 for the Session::get().

I also tried to use withErrors() in the Redirect, but none of my errors never passed through for some reason. I should point out this call is tied into the default routes, except forsetUserPassword method, which maybe overriding session data higher up in the run order. These changes are not made to any custom routes.

The complete setUserPassword() method, inside of Controllers/Auth/ResetPasswordController.php follows:

Any ideas on how to fix this would be great, its a significant blocker for our project. Thanks.

0 likes
2 replies
LaryAI's avatar
Level 58

To address the issue of the flash message not being displayed on your reset password page, let's go through a few potential solutions and checks:

  1. Ensure Session is Started: Make sure that the session is properly started in your application. Laravel should handle this automatically, but it's worth checking if there's any custom middleware or configuration that might be affecting session handling.

  2. Check Middleware: Ensure that the web middleware group is applied to your routes. This middleware is responsible for starting the session and sharing session data with views.

    Route::group(['middleware' => ['web']], function () {
        // Your routes here
    });
    
  3. Redirect Properly: When using Redirect::back(), ensure that the request is actually being redirected to the intended route. You might want to log the URL to confirm this.

  4. Use with() for Flash Data: Instead of using Session::flash(), you can pass flash data directly with the redirect using the with() method. This ensures that the data is available in the session for the next request.

    return redirect()->back()->with('flash', 'Entra Error');
    
  5. Blade Template Check: Ensure that your Blade template is correctly checking for the session data. The print_r function is not necessary for displaying the session data. You can directly output the session variable.

    @if (session('flash'))
        <div class="alert alert-danger" role="alert">
            There was an Error setting your password. Please contact support.
            {{ session('flash') }}
        </div>
    @endif
    
  6. Debugging: Add some logging or debugging statements to ensure that the code path is being executed as expected. For example, log the session data right before rendering the view to confirm it's set correctly.

  7. Check for Overwrites: Ensure that no other part of your application is overwriting the session data before it reaches the view. This could happen if there are multiple redirects or session manipulations.

By following these steps, you should be able to diagnose and fix the issue with the flash message not appearing on your reset password page. If the problem persists, consider checking the server logs for any errors or additional clues.

CKilgore87's avatar

I also wanted to point out that the behavior for Password Reset that I am trying to alter ties into Laravel UI's password reset, which may mean my options to override this behavior are limited without changing what's either in vendor, or be completely rebuilding the password reset from scratch. I really do not want to do either, and my time is limited here. What can be done to make this work with Redirects?

Please or to participate in this conversation.