如何在laravel blade模板中显示消息包object?

how to display message bag object in laravel blade template?

我想在 Laravel blade 模板中显示我的错误。我该怎么办?

return view('edit', [
    "id"      => $request->id,
    "data"    => $data,
    "errors"  => $validator->messages(),
    "success" => null,
]);

预期输出为

The mahajan first name may only contain letters.

但是如果我打印 $errors.

就会显示
{
  "mahajan_first_name":["The mahajan first name may only contain letters."],
  "mahajan_middle_name":["The mahajan middle name may only contain letters."],
  "mahajan_last_name":["The mahajan last name may only contain letters."]
}

您可以像这样在无序列表中打印它们:

@foreach ($errors->all() as $error)
   <li>{{ $error }}</li>
@endforeach

单个错误显示如下:

{{ $errors->first('mahajan_first_name') }}

要检查是否存在错误,您可以这样做:

@if($errors->has('mahajan_first_name'))
 // your code here
@endif