Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
Blade ternary operator without else

I've got a object which has the method hasATest that returns a boolean and depending on the value I want a button to be enabled or disabled so I thought of 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 about the else. If I remove the :, an error occurs:

"unexpected ="  ... 

Plus it's not like there's the opposite for disabled.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The ternary operator needs an else as you already discovered, you could try some statements like null or in this case "" to return empty values on the else.

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

about 4 years ago · Santiago Trujillo Relatório

0

Just use an empty string 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 also use an @if for it instead of a ternary.

<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>
about 4 years ago · Santiago Trujillo Relatório

0

You have problem in this line:

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

Here is the solution:

{{ ($question->hasATest()) ? disabled : 'enable' }}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda