php - Laravel 5.4在 View 中显示错误

标签 php laravel-5 error-handling

我有一个上载文件的表格。
在 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/

相关文章:

php - 如何使用php增加带有前导零的值

php - 迭代数组时如何检测循环?

c# - C#异常处理程序继续下一个

go - 将外部错误映射到 golang 中的域错误

php - 两次声明相同的路由,但根据中间件期望不同的行为

php - 怎么在word之间加空格

laravel-5 - Laravel Eloquent 如何选择和操作数据透视表中的行?

php - 优化数据库查询 - 扩展大数据 - Laravel 5

javascript - 在 Laravel 5 中使用 Ajax 并返回 json 数组

.net - ASP.NET Web API IExceptionLogger 调用两次