在 laravel 中,为了一次显示所有错误消息,我在 View 中使用了以下代码
<?php
$something = $errors->all();
if(!empty($something)):
?>
<div class = "alert alert-error">
@foreach ($errors->all('<p>:message</p>') as $input_error)
{{ $input_error }}
@endforeach
</div>
<?php endif; ?>
但是当我想在 if 条件下使用 $errors->all()
而不是 $something
时它显示错误
Can't use method return value in write context
虽然上面的代码工作正常,但我认为可能有更好的方法来检查是否存在任何错误消息,如果存在则显示它。
最佳答案
是的,因为您不能将任何方法用作空函数参数。来自 php 文档:
empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.
$errors 是什么类?如果它是您自己的类,您可以实现类似“isEmpty()”的方法,然后在 if 语句中使用:
if ($errors->isEmpty()) { ...
关于php - 检查是否存在任何错误消息并在 laravel 中显示所有错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12419814/