The last couple of days, I face a problem. Validation errors variable comes empty inside blade files.. I am developing a multilingual application in Laravel 5.2.45. So there are the validation error messages in each of them (resources/lang/{locale}/validation.php).
Validation rules live inside a Request file (e.g. ValidateUserRequest) containing the validation rules and declaring authorize as true. I then pass it to the controller. Bellow is my middlewares and part of my routes file.
Any help will be much appreciated
Kernel.php
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',
],
];
protected $routeMiddleware = [
'auth_pabl' => \App\Http\Middleware\AuthenticateBackEnd::class,
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
routes.php (all controllers inside Admin directory)
Route::auth();
Route::group ( [
'namespace' => 'Admin'
],
function () {
Route::get ( 'admin/dashboard', [
'uses' => 'PanelController@dashboard',
'as' => 'admin.dashboard'
] );
/*
* Authors
*/
Route::get ( 'admin/authors', [
'uses' => 'PanelController@authors',
'as' => 'admin.authors'
] );
Route::post ( 'admin/authors', [
'uses' => 'AuthorController@store',
'as' => 'admin.authors.store'
] );
Route::get ( 'admin/authors/{slug}/edit', [
'uses' => 'AuthorController@edit',
'as' => 'admin.authors.edit'
] );
Route::post ( 'admin/authors/{username}/edit', [
'uses' => 'AuthorController@update',
'as' => 'admin.authors.update'
] );
Route::get ( 'admin/authors/{username}/delete', [
'uses' => 'AuthorController@delete',
'as' => 'admin.authors.delete'
] );
A session message would normally come from something like
\Session::flash('flash_contact', 'Success. Data stored');
return redirect()->route('admin.events.create');
And it is (not) displayed with
@if($errors->count()>0) <br /> <br />
<div id="#problem" class="alert alert-danger text-pull-left">
<p>{!! trans('admin/form/placeholders.errors') !!}</p>
<ul class="errors">
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
@if(Session::has('flash_contact'))
<div id="success" class="alert alert-success text-center">
{{Session::get('flash_contact')}}
</div>
@endif
Finally php artisan route:list states that the "web" middleware is present (I have not declared it twice manually)
//Edit 1 (withErrors from controller)
Note here that neither function withErrors give me the expected result. Below is a part of my controller.
public function store(Requests\ValidateEventsRequest $request)
{
try {
return $this->dbEvent->add($request);
} catch (\Exception $e) {
return redirect()->to('/admin/events/add')->withErrors('Error
detected');
}
}
//Edit 2
Shouldn't this work???
Route::get('flash', function () {
return redirect()->to('flash2')->withErrors('Where are my errors?');
});
Route::get('flash2', function () {
return view('flash2');
});
And my flash2.blade.php
<html>
<body>
@if($errors->count()>0) <br /> <br />
<div id="#problem" class="alert alert-danger text-pull-left">
<p>{!! trans('admin/form/placeholders.errors') !!}</p>
<ul class="errors">
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
this is my flash page
</body>
</html>