Laravel關於$errors變數的問題

來源:互聯網
上載者:User
當我在視圖中使用$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:

  1. In your kernel.php file, you can move the middleware \Illuminate\View\Middleware\ShareErrorsFromSession::class back to the protected $middleware property.

  2. 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')

樓主解決得不錯,贊一個

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.