Laravel 5.2 $errors not appearing in Blade
So I'm following along with the Laravel 5 fundamentals tutorial and I am stuck on the form validation. I have followed along exactly with the tutorial but I am getting a Undefined variable: errors in my create articles view.
In the tutorial I am following and what I have found online they say the errors variable is always there in the blade file for you to use so I don't know what i am doing wrong?
Any help would be appreciated! loving Laravel except for this error!
View
@if($errors->any())
<ul class="alert alert-danger">
@foreach($errors->any() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
@endif
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Http\Requests;
use App\Http\Requests\UserRequest as UserRequest;
// use App\Http\Requests\CreateArticleRequest as CreateArticleRequest;
use App\Http\Controllers\Controller;
use Illuminate\View\Middleware\ErrorBinder;
class UserController extends Controller
{
public function create(){
return view('pages.signUp');
}
public function store(UserRequest $request){
User::create($request->all());
return 'the user has been registered!';
return view('user.profile');
}
}
Request validation
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class UserRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'country' => 'required',
'password' => 'required|min:6',
'confirm_password' => 'required|same:password',
'height' => 'required',
'weight' => 'required',
];
}
}
This is a breaking problem with the 5.2 upgrade. What's happening is the middleware which is responsible for making that errors
variable available to all your views is not being utilized because it was moved from the global middleware to the web
middleware group.
There are two ways to fix this:
In your
kernel.php
file, you can move the middleware\Illuminate\View\Middleware\ShareErrorsFromSession::class
back to theprotected $middleware
property.-
You can wrap all your web routes with a route group and apply the
web
middleware to them.Route::group(['middleware' => 'web'], function() { // Place all your web routes here... });
Solved
You may change any one of the following:
1. put your working route (app/http/routes.php)
on
Route::group(['middleware' => ['web']], function () {
// Here like
Route::get('/', 'TodoController@index');
Route::post('/', 'TodoController@store');
});
Screenshot -
2. Move your protected $middlewareGroups web
(app/Http/Kernel.php)
on protected $middleware = []
Screenshot -