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

Web Developer
Remote
Livesystems dooh AG
Senior Backend Engineer
Remote
64 Robots
Senior Laravel Developer
Atlanta, GA
Helium LLC
Full-Stack Developer (JS - Vue.js, PHP - Laravel)
US Remote / Telecommute
TTEC Digital
Backend Engineer
Brooklyn, NY
Stationhead
Back-End Developer
Orlando, FL
Christ for all Nations
Senior Software Developer
Nashville, TN
Bernard Health