我试图在div中显示所有错误。
控制器
if ($validator->fails()) {
return redirect('/')->withErrors($validator->errors);
}
查看
@if($errors->any())
@foreach($errors->all() as $error)
{{ $error }}
@endforeach
@endif
我得到了:
未定义的变量:错误
答案 0 :(得分:5)
你需要这个:
if ($validator->fails()) {
return redirect('/')->withErrors($validator->errors());
}
注意函数调用errors()
而不是引用变量。
要使用此功能,您必须通过在$app->middleware()
文件的bootstrap/app.php
方法调用中取消注释中间件来启用会话。
答案 1 :(得分:3)
$errors
在我的观点中也未定义,直到我在bootstrap\app.php
中取消注释以下几行:
$app->middleware([
Illuminate\Session\Middleware\StartSession::class,
Illuminate\View\Middleware\ShareErrorsFromSession::class,
]);
目前,有关验证的Lumen文档告诉您视图中的$ errors变量始终。显然,当你“开箱即用”时使用它并非如此。