在laravel中,一次显示所有错误消息,我在视图中使用以下代码
<?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 docs:
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.
什么类是$错误?如果这是你自己的类,你可以实现像’isEmpty()’这样的方法,然后在if语句中使用:
if ($errors->isEmpty()) { ...