Blade “or” Operator
Published on by Eric L. Barnes
The following is an excerpt from a previous issue of my weekly Laravel Newsletter. Signup and have useful tips like this delivered right to your inbox.
This week I came across a question about ternaries in Laravel blade. Well, not asking that exactly, but that was the premise. I replied about the use of the “or” operator and apparently a few people didn’t know about it. So I’m making it this weeks tip of the week.
The basic usage of this used to be the or
operator but that changed in Laravel 5.7 to ??
:
-{{ $name or 'John' }} +{{ $name ?? 'John' }}
Which compiles into:
isset($name) ? $name : 'John'
This is one of those nice little features that is easy to skip over in the documentation, but it is really great for keeping your views clean.
Eric is the creator of Laravel News and has been covering Laravel since 2012.