Connor Leech
Connor Leech

Reputation: 18833

Laravel session flash messages are not showing on production (Laravel Forge)

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?

Upvotes: 1

Views: 2284

Answers (3)

Connor Leech
Connor Leech

Reputation: 18833

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

Upvotes: 0

Vipertecpro
Vipertecpro

Reputation: 3274

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

Upvotes: 0

Vidal
Vidal

Reputation: 2621

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

Upvotes: 0

Related Questions