If statement in Laravel blade template


Blade template have it's own way of doing conditional 'if' statement, the syntax is little bit different from the regular php 'if' statement, but works the same way.

Here's the syntax of 'if' statement on blade template:
<?php $data = 'car'; ?>

@if ($data == 'car')
    {{{ 'i have a car' }}}
@endif

As you can see the 'if' statement on blade is start with '@if' and close with '@endif', this syntax maybe little bit weird at first, but you just need to get used to it.

Here's another example 'if' statement with 'elseif' and 'else':
<?php $data = 'airplane'; ?>

@if ($data == 'car')
    {{{ 'i have a car' }}}
@elseif ($data == 'house')
    {{{ 'i have a house' }}}
@else
    {{{ 'i have nothing' }}}
@endif

Besides the 'if' statement, there is also 'unless' statement on blade template, which the opposite of 'if', here's an example: 
@unless (Auth::check())
    You are not signed in.
@endunless

The 'unless' statement will check for false value, if it's false the code below will be executed, the 'Auth::check' will return true or false, if the user is login it will return true otherwise false.


EmoticonEmoticon