Laravel echoing object property after checking for Existence

☆樱花仙子☆ 提交于 2019-12-13 02:33:57

问题


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

{{ $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->property or 'Default' }}

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

{{ 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?


回答1:


You can use.

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

The "@" is a Error Control Operator

I haven't seen a Laravel specific method.



来源:https://stackoverflow.com/questions/28554946/laravel-echoing-object-property-after-checking-for-existence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!