我有一个上载文件的表格。
在 Controller 中,我对该文件中的列进行了各种检查,并得到一些错误。
我将这些错误添加到数组中,并希望在 View 中显示所有这些错误。
我尝试了各种解决方案,但没有任何效果。
现在,我正在 Controller 中针对文件中的每一行执行此操作:
$errors[] = array('file_name'=>$file_name, 'error'=>'Invalid coffee name');
在 View 中,我尝试这两件事:
@if ($errors->any())
{{ implode('', $errors->all('<div>:message</div>')) }}
@endif
@if ($errors->any())
@foreach ($errors->all() as $error)
<div>{{$error}}</div>
@endforeach
@endif
问题是,尽管我在errors数组中有2个错误(我检查过),但我只在 View 中看到最后一个。
我究竟做错了什么?
最佳答案
我这样解决了:
在我的 Controller 方法中,我添加了:
$errors = new MessageBag();
当我遇到错误时:
$errors->add('coffee', $file_name . ': Invalid coffee name');
在 View 中:
@if ($errors->any())
<div class="alert alert-danger">
<p>There are errors in the file you uploaded</p>
<ul>
@foreach ($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
@endif
关于php - Laravel 5.4在 View 中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705027/