首页 > 解决方案 > @error 指令未在 laravel 视图中显示错误

问题描述

我正在研究自定义 Laravel 登录实现。我从控制器返回错误如下:

$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
  // Authentication passed...
  return redirect()->intended('surveys');
}else {
  return redirect()->back()->withErrors(['email', 'The Message']);
}

这是我提取此错误的视图实现:

<div class="col-md-6">
   <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
@error('email')
  <span class="invalid-feedback" role="alert">
      <strong>{{ $message }}</strong>
  </span>
@enderror
</div>

我只想用这种方式。由于代码最小化,不是显示错误的其他方法。我正在使用 Laravel 7.0+

标签: phplaravel

解决方案


请再试一次:

$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
  // Authentication passed...
  return redirect()->intended('surveys');
}else {
  return redirect()->back()->withErrors(['email' => 'The Message']);
}

推荐阅读