Ternary Blade operator without the other

I have an object that has a method hasATest

that returns a boolean and depending on the value I want the button to be enabled or disabled, so I thought about doing something like this:

<button class="btn btn-xs btn-detail btn-activate" name="question_id" value="{{$question->id}}" id="activate{{$question->id}}"
    "{{ $question->hasATest() ? disabled : }}"> Activate 
</button>

      

But I don't know what to do with the other. If I delete :

, an error occurs:

"unexpected ="  ... 

      

Plus it doesn't feel like the opposite for the disabled.

+3


source to share


3 answers


The ternary operator needs else

, as you've already discovered, you can try multiple type operators null

or in this case ""

to return null values ​​in else

.



{{ ($question->hasATest()) ? "disabled" : "" }}

+3


source


You have a problem on this line:

"{{ $question->hasATest() ? disabled : }}"

      



Here's the solution:

{{ ($question->hasATest()) ? disabled : 'enable' }}

      

+1


source


Just use an empty line for the else part.

<button class="btn btn-xs btn-detail btn-activate" name="question_id" value="{{$question->id}}" id="activate{{$question->id}}"
    {{ $question->hasATest() ? 'disabled' : '' }}> Activate 
</button>

      

I think you could use @if

ternary for it instead.

<button class="btn btn-xs btn-detail btn-activate" name="question_id" value="{{$question->id}}" id="activate{{$question->id}}"
    @if($question->hasATest()) disabled @endif> Activate
</button>

      

+1


source







All Articles