當我在視圖中使用$errors時,laravel會報錯,提醒$errors未定義,伺服器端環境為Homestead4.0.0,Laravel版本為5.2。
我看5.2手冊中說明$errors在每個視圖中均可用啊,請問這是怎麼一回事啊?
回複內容:
當我在視圖中使用$errors時,laravel會報錯,提醒$errors未定義,伺服器端環境為Homestead4.0.0,Laravel版本為5.2。
我看5.2手冊中說明$errors在每個視圖中均可用啊,請問這是怎麼一回事啊?
問題解決了,雖然不是@rainwsy和@hi兩位說的問題,但是很感謝回答。
造成這個問題的原因是5.2源碼改動,但是手冊裡面又沒有寫清楚,或者我沒有看清楚,現將解決辦法貼下來,供有相同需求的朋友一覽:
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 the protected $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... });
原文地址:Laravel 5.2 $errors not appearing in Blade
你要判斷有錯誤才輸出顯示
@if (count($errors) > 0) Whoops! There were some problems with your input.
@foreach ($errors->all() as $error)
- {{ $error }}
@endforeach
@endif
https://laravel.com/docs/5.2/quickstart-intermediate#validation
The $errors Variable
是不是還要引入一個通用檔案 @include('common.errors')
樓主解決得不錯,贊一個