Laravel检查存在后回显对象属性 [英] Laravel echoing object property after checking for Existence

查看:60
本文介绍了Laravel检查存在后回显对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel中,有一种用于检查和打印变量(如果存在)的快捷方法:

In Laravel there is a shortcut method for checking and printing variable if it exists:

{{$ name或'Default'}}

{{ $name or 'Default' }}

对象属性是否有类似的语法?因为这种语法会抛出错误,以防万一我尝试检查对象属性是否存在:

Is there any similar syntax for object properties? Because that kind of syntax will throw an error in case I'll try to check an object property for existence:

{{$ object->属性或默认"}}

{{ $object->property or 'Default' }}

我知道我可以使用普通的PHP来解决该问题:

I know I can solve that with plain PHP like that:

{{isset($ object-> property)吗? $ object->属性:默认"}}

{{ isset($object->property) ? $object->property: 'Default' }}

但这并不奇怪吗?对象属性比变量长,并且它们还必须使用更长的语法?

But isn't that odd? Object properties are longer than variables and they also have to use longer syntax?

推荐答案

您可以使用.

{{ @$object->property ?: 'Default'}}

"@"是错误控制运算符

我还没有看到Laravel的特定方法.

I haven't seen a Laravel specific method.

这篇关于Laravel检查存在后回显对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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