Undefined variable: errors -- Laravel 5.2
I am new in Laravel and using laravel version 5.2.
I created a controller and request named as ArticlesController and CreateArticleRequest respectively and i defined some validation rules.
CreateArticleRequest
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateArticleRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'title' => 'required|min:3',
'body' => 'required|max:400',
'published_at' => 'required|date',
];
}
}
ArticlesController
<?php
namespace App\Http\Controllers;
use App\Article;
//use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use App\Http\Requests\CreateArticleRequest;
class ArticlesController extends Controller
{
public function add(){
return view('articles.add');
}
public function create_article_row(CreateArticleRequest $request){
Article::create($request->all());
return redirect('articles/');
}
}
When i use $errors variable in my template named as add.blade.php it show error undefined variable: $errors I tried to solve the problem but i did't .Please tell me where i am wrong . add.blad.php
{{ var_dump($errors) }}
Answer
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(app/Http/Kernel.php), you can move themiddleware \Illuminate\View\Middleware\ShareErrorsFromSession::class
back to theprotected $middleware
property.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...(Cut all `Route` which are define in `Route file`, paste here) });
Copied from this post Laravel 5.2 $errors not appearing in Blade
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?