Add Selected to option after equality check

Asked

Viewed 53 times

0

I have the following select on a form in the Laravel:

<div class="form-group has-feedback {{ $errors->has('nivel') ? 'has-error' : '' }}">
    <label for="nivel" class="col-sm-2 control-label">Nível de acesso</label>
    <div class="col-sm-10">
        <select class="form-control" name="nivel" value="{{ $detailpage->nivel }}" placeholder="Nivel de acesso">
            @if($detailpage->nivel != 2) <option value="2">Operacional</option>
            @else <option value="2" selected>Operacional</option>
            @endif
            @if($detailpage->nivel != 1) <option value="1">Administrativo</option>
            @else <option value="1" selected>Administrativo</option>
            @endif
        </select>
    </div>
</div>

This is an example, in practice there will be several categories.

Is there any way to put something like:

<option value="1" @if($detailpage->nivel != 1) selected >Administrativo</option>

That is to say that the item with the value == nivel sera selected and the others not

But in a simpler way than I used

1 answer

1


Untested, but it might look something like this

<div class="form-group has-feedback {{ $errors->has('nivel') ? 'has-error' : '' }}">
    <label for="nivel" class="col-sm-2 control-label">Nível de acesso</label>
    <div class="col-sm-10">
        <select class="form-control" name="nivel" value="{{ $detailpage->nivel }}" placeholder="Nivel de acesso">
 <option value="2"{{ $detailpage->nivel == 2 ? "selected='selected'" : "" }}>Operacional</option>

...

  • Thanks, it worked

  • Corrected the != by ==

Browser other questions tagged

You are not signed in. Login or sign up in order to post.