1

I have a Laravel Blade form that is not displaying flash session error messages on my production site. It was previously giving 419 error response "Sorry, your session has expired. Please refresh and try again. on production laravel". I was able to clear that so the form submits by clearing the cache and composer dump-autoload.

This is the form to display the sessions. Works locally, I am on L5.7.9.

<form method="POST" action="{{ route('candidates.store') }}" class="form">
    @csrf

    @if(session('message'))
        <div class="alert alert-success">
            {{ session('message') }}
        </div>
    @endif

    @if (session('login_error'))
        <div class="alert alert-danger" role="alert">
            {{ session('login_error') }}
        </div>
    @endif

    @if ($errors->any())
        <div class="alert alert-danger" role="alert">
            Woops had some problems saving
        </div>
    @endif

In my .env file I have:

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_DOMAIN=employbl.com
QUEUE_DRIVER=sync

Then in the controller store method I have:

public function store(Request $request)
{
    $validator = $request->validate([
        'first_name'         => 'required',
        'last_name'          => 'required',
        'email'              => 'email|required|unique:users',
        'linkedin_url'       => 'required|url',
        'phone_number'       => 'required',
        'work_authorization' => 'required',
        'city'               => 'required',
        'state'              => 'required'
    ]);

    $candidate         = new User($request->all());
    $candidate->active = false;
    $candidate->save();

    return redirect()->route('candidates.landing')->with('message', 'Successfully applied to join Employbl network!');
}

For routes I do not have a route group:

Route::get('/candidates', 'CandidateController@landing')->name('candidates.landing');
Route::post('/candidates', 'CandidateController@store')->name('candidates.store');

php artisan route:list shows that I am only using the web middleware once:

|        | POST     | candidates                                                                                 | candidates.store            | App\Http\Controllers\CandidateController@store                             | web                                                                                                                                                                               |

The flash messages are working locally, but when I submit the form on production (deployed with Laravel Forge) the form submits but no flash messages are displayed. What can I do to make the session messages appear on production and why is this happening?

Connor Leech
  • 18,052
  • 30
  • 105
  • 150
  • Please check of [this](https://stackoverflow.com/questions/34648930/laravel-session-flash-message-not-working) helps – Mihir Bhende Feb 20 '19 at 14:36
  • Also did you do `php artisan migrate` to have a `sessions` table on production **if** your session driver is `database`? – Mihir Bhende Feb 20 '19 at 14:37
  • Updated to show my routes info, not issue there. My session driver on prod is `file` so don't have a sessions table. Would using a sessions table possibly help with this? – Connor Leech Feb 20 '19 at 14:42
  • Just checking if the normal login works? Like not flash messages but normal session? Might be file permissions for `storage/framework/sessions`. Mostly check permissions of `storage` folder? – Mihir Bhende Feb 20 '19 at 14:50
  • Okay I updated database to use sessions table. The session messages are still not showing when I submit the form. I am able to login and logout from the app fine though. With using session db table would file permissions still be the issue? – Connor Leech Feb 20 '19 at 14:53
  • Where are you setting `session('message')` and `session('login_error')` or those are validation erros? – Mihir Bhende Feb 20 '19 at 14:57
  • Session message is validation error. Even if it wasn't set I'd be hoping `$errors->any()` would catch errors. As it is now when I click submit even with no input I don't get any flashes: https://employbl.com/candidates – Connor Leech Feb 20 '19 at 15:08
  • I just realised you passing `message` using `with()`. So you can use that using `$message` directly – Mihir Bhende Feb 20 '19 at 15:11
  • The flash data will exists until the next request, you're sending the message to a "route" and them calling the view in this route, so tem data will not exists on the view. Try to access the $message on the "landing" function – JoaoGRRR Feb 20 '19 at 17:06

3 Answers3

0

You are missing some methods (has) to check if you have a session with that name and (get) to get the value.


@if(session::has('message'))
        <div class="alert alert-success">
            {{ session::get('message') }}
        </div>
    @endif

    @if (session::has('login_error'))
        <div class="alert alert-danger" role="alert">
            {{ session::get('login_error') }}
        </div>
    @endif



    @if ($errors->any())
        <div class="alert alert-danger" role="alert">
            Woops had some problems saving
        </div>
    @endif
Vidal
  • 2,605
  • 2
  • 16
  • 32
  • Hey Vidal, this is not the issue. Using session helper like I have above is fine: https://laravel.com/docs/5.7/session#using-the-session. It works locally as well so I think it's something with session config on my server. Thank you for the input though – Connor Leech Feb 20 '19 at 15:07
  • Got it, I will do an dd on the blade to see if we have a value on the session while on the blade template. – Vidal Feb 20 '19 at 15:20
0

Is that the problem ?

     return redirect()
            ->route('candidates.landing')
            ->with('message', 'Successfully applied to join Employbl network!');
    // I think with takes [](array) so, 
     return redirect()
            ->route('candidates.landing')
            ->with(['message' => 'Successfully applied to join Employbl network!']);
  //
  //
  // But why are't you doing like this 
  //
  //
  return back()->with([
          'message' => 'Successfully applied to join Employbl network!'
        ]);
  //
  //
  //
  // And yes Session::has('message') or \Session::has('message') is a good way to check in the blade
Vipertecpro
  • 3,056
  • 2
  • 24
  • 43
0

It turns out I needed to run php artisan cache:clear to get rid of existing session information. More info here: https://laracasts.com/discuss/channels/forge/419-error-when-submitting-form-in-production-sorry-your-session-has-expired-please-refresh-and-try-again#reply=494157

Connor Leech
  • 18,052
  • 30
  • 105
  • 150