php - Laravel 刀片“调用数组上的成员函数 has()”
问题描述
控制器:
session(['errors' => ['email' => ['The email is invalid.']]]);
return view('auth.login');
刀:
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
错误:
调用数组上的成员函数 has()
我在数组之前尝试过 (object),return view()->with() 等等!但我总是得到这个错误!
如果可能的话,我不想更改刀片文件!反正有没有以正确的方式从控制器发送数据?
解决方案
Validator 返回的 $errors 是 Illuminate\Support\MessageBag 的一个实例,而不是一个数组;
要复制用法:在您的控制器中,您可以:
use Illuminate\Support\MessageBag;
// Create a new MessageBag instance in your method.
$errors = new MessageBag;
// Add new messages to the message bag.
$errors->add('email', 'The email is invalid.');
return view('auth.login', ['errors' => $errors]);
我认为您应该在刀片模板中使用另一个变量名称,例如 $customErrors ,以确保将来可以在需要时使用 view('view')->withErrors($validator) ,因为 withErrors 将变量 $errors 传递给查看模板. https://laravel.com/docs/5.8/validation#working-with-error-messages
推荐阅读
- python-3.x - 预测和类激活图仅适用于某些图片
- css - 我的折叠菜单列表未显示在其父级之外。菜单列表扩展了父级,但不是每个菜单中的列表
- php - 多次插入数据
- python - pyYAML 中的自定义转储程序
- python - 错误是 exec(compile(f.read(), filename, 'exec'), namespace) 。第 26 行 ---- def__init__(self, battery_size = 70):
- java - 如何将 .crt 文件添加到密钥库和信任库
- r - 如何将 R 中的过滤子集保存在新列中?
- python - 从 0 到 n 每隔一个整数相加(递归函数)
- c++ - 未知大小的数组作为类成员,用于在运行时制作数组对象(对象创建时)
- apache-flink - 对随机数源中的数字求和