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


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior Quality Assurance Engineer
Remote
Bisnow Media
Senior PHP/Laravel Developer: Your Dream Work Environment
Remote
iPhone Photography School
Senior Software Engineer
Remote or San Francisco
Curology
Laravel Developer
Las Vegas, NV
V Shred
R&D Developer
Denver, CO
booj
Full-Stack Developer
Paris, France
Wingly
Full Stack Developer
Remote US-Only
atlasMind