Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ternary in Laravel Blade

Looking for a ternary operator for blade templates

@if(Auth::check()) ? yes : no @endif 

Can't seem to get it to work this works

@if(Auth::check()) yes @else no @endif 

suppose there is not much in it for this example, just curious.

like image 979
LeBlaireau Avatar asked Aug 13 '14 10:08

LeBlaireau


People also ask

What is ternary operator in laravel?

The ternary operator is a conditional operator that decreases the length of code while performing comparisons and conditionals. This method is an alternative for using if-else and nested if-else statements.

How is the ternary conditional operator used in PHP?

The term "ternary operator" refers to an operator that operates on three operands. An operand is a concept that refers to the parts of an expression that it needs. The ternary operator in PHP is the only one that needs three operands: a condition, a true result, and a false result.

What is the format of conditional operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.


2 Answers

You are free to use it with {{ }}.

{{ Auth::check() ? 'yes' : 'no' }} 
like image 94
Marwelln Avatar answered Oct 04 '22 16:10

Marwelln


This works:

{{ Auth::check() ? 'yes' : 'no' }} 
like image 20
Laurence Avatar answered Oct 04 '22 14:10

Laurence