问题
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