php - Laravel关于$errors变量的问题
天蓬老师
天蓬老师 2017-04-10 16:34:32
[PHP讨论组]

当我在视图中使用$errors时,laravel会报错,提醒$errors未定义,服务器端环境为Homestead4.0.0,Laravel版本为5.2。
我看5.2手册中说明$errors在每个视图中均可用啊,请问这是怎么一回事啊?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(4)
高洛峰

问题解决了,虽然不是@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)
<p class="alert alert-danger">
    <strong>Whoops!</strong> There were some problems with your input.<br>
    <br>
    <ul>
        @foreach ($errors->all() as $error)
        <li>{{ $error }}</li> 
        @endforeach
    </ul>
</p>
@endif
怪我咯

https://laravel.com/docs/5.2/quickstart-intermediate#validation
The $errors Variable
是不是还要引入一个通用文件 @include('common.errors')

伊谢尔伦

楼主解决得不错,赞一个

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号