Blade “or” Operator

The following is an excerpt from a previous issue of my weekly Laravel News Digest. 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 is:

{{ $name or '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.

This also works in Envoy tasks:

@task('dev', ['on' => ['dev']])  
    git checkout origin/{{ $branch or 'develop' }}

You would call this with either of these:

envoy run dev  
envoy run dev --branch=foo

Nice and simple!

Filed in: Laravel Tutorials
Laravel News Partners

Laravel Jobs

Senior Full-Stack Developer
Atlanta, GA - (remote) USA ONLY
Curricula
PHP Laravel Developer
London
Investoo
Senior Software Engineer
Boston, MA
ButcherBox
Senior Laravel Developer
London
Geomiq Ltd
Remote Full Stack Laravel / Vue Developer
Remote
EventForge LLC

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.