Append selected to option after equality check

0

I have the following select in a form in 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>

In other words, the item with value == nivel will be selected and the others will not

But simpler than the one I used

    
asked by anonymous 28.11.2017 / 19:16

1 answer

1

Not tested, but might look like something

<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>

...

    
28.11.2017 / 19:46