如果语句在刀片模板中? [英] If statements in blade templating?

查看:47
本文介绍了如果语句在刀片模板中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组空数据,在我的刀片服务器模板中,我通过以下方式进行检查:

I have an empty set of data, in my blade template I check for this via:

@if(empty($data))
   It's empty!
@else
   @foreach($data as $asset)
      $asset->alt
   @endforeach
@endif

问题是,我得到了错误:

The problem is, I get the error:

Trying to get property of non-object

但是这甚至不应该发生,因为if语句的else甚至不应该运行.为什么会这样?

But this should not even happen as the else of the if statement should not even run. Why is this happening?

我已检查并显示它为空".

I've checked and 'It's empty' is displayed.

推荐答案

您可以使用

@if(count($data))
   @foreach($data as $asset)
      {{$asset->alt}}
   @endforeach
@else
   It's empty!
@endif

这篇关于如果语句在刀片模板中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆