PamanBeruang
PamanBeruang

Reputation: 1589

Laravel much better of using if

so most of time i do this

@if(!empty($variable)
  @if($variable == "yes")
      do something here
  @endif
@endif

well as you can see it really not a beautiful way, maybe (i really hope there is) a much simpler way to just detect if the variable is exist and not throwing error when it is not exist beside that...?

it making my code so crowded and if by anychance i forgot to add those will got beautiful error (well i design a eye cathing error page by the way)

@if(!empty($variable))
or 
@if(isempty($variable))

Upvotes: 0

Views: 51

Answers (2)

Sid
Sid

Reputation: 5833

if you are searching for one line condition, it will work as expected,

  {{!empty($variable) ? $variable == 'yes' ? 'do something' : 'do something else' : 'variable is empty'}}

Upvotes: 0

Joseph Silber
Joseph Silber

Reputation: 219938

Combine the two checks into a single if statement:

@if (! empty($variable) && $variable == 'yes')
    do something here
@endif

Upvotes: 4

Related Questions