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

himowa's avatar

5.2 Session data not showing in views

Validation errors are not being stored on my Laravel 5.2 application (new installation, not upgraded). In addition, $old('value') is not retaining the values after validation fail.

All my routes are contained in the web middleware, and I also tried moving the web middleware contents to the global $middleware variable in Kernel.php.

I have also attempted to clear my route cache in artisan and include use Illuminate\Foundation\Validation\ValidationException at the top of my controller to see if i could make it work. It seems like the validation is processing, it's just not storing it in the session/throwing the exception. I say this because Session::flash is also not working in the same way and the user remember me feature doesn't seem to be storing as long as the cookie says it does, as I am required to re-log in after about 6 hours (though this may just be a change from 5.1).

Routes Setup (I have "regular" public routes and then a staff route:

Route::group(['middleware' => ['web']], function () {
// Basic Route...
 $this->get('/', 'PublicController@index');

 // Staff Route...
  Route::group(['middleware' => 'staff'], function () {
  $this->get('/staffpanel', 'Staff\StaffController@showStaffPanel');
 });
});

Kernel.php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\VerifyCsrfToken::class,
];

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'staff' => \App\Http\Middleware\Staff::class,
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
}

Validation snippet:

protected function validatorMakeCategory(array $data) {
  return Validator::make($data, [
    'name'           => 'required|max:50|unique:categories',
    'description'    => 'max:255',
  ]);
}

public function postCreateCategory(Request $request) {
  $validator = $this->validatorMakeCategory($request->all());

  if ($validator->fails()) {
    $this->throwValidationException(
      $request, $validator
    );
  }
}

Errors/Session in views (neither of which show):

   @if(Session::has('message'))
          <p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ Session::get('message') }}</p>
      @endif

      @if (count($errors) > 0)
        <div class="alert alert-danger">
          <ul>
            @foreach ($errors->all() as $error)
              <li>{{ $error }}</li>
            @endforeach
          </ul>
        </div>
      @endif
0 likes
2 replies
Snapey's avatar

you should not need to mess with kernel.php

in routes.php change instances of $this->get to Route::get

convert your validation classes into request classes.

once it looks more like 'convention' not only might it work, but also more people will be able to help

Snapey's avatar

also, I can't see in your routes file how you get to postCreateCategory?

Please or to participate in this conversation.