Return select field value in Laravel

Asked

Viewed 1,487 times

1

How do I return the value that is in the database of a select field and continue showing the other options to edit:

      <div class="form-group">
        <label>Tipo de imóvel</label>
        <select name="tipo_id" class="form-control">
            <option selected="disabled">Selecionar</option>
          @foreach($types as $value)
            <option>{{$value->nome}}</option>
          @endforeach
        </select>
      </div>
  • What is the model, or code you are using?

  • Which model are you talking about? I’m putting it this way and it returns only the id instead of the name: <select name="type_id" class="form-control"> <option>{$Property->type_id}</option> @foreach($types as $value) <option>{{$value->name}</option> @endforeach </select>

  • Marcelo I’m saying before sending to View, the code of your Controller?

  • http://pastebin.com/TrwMtWNy

1 answer

1


I imagine what you’re looking for is this:

 <div class="form-group">
    <label>Tipo de imóvel</label>
    <select name="tipo_id" class="form-control">
        <option disabled="disabled">Selecionar</option>
      @foreach($types as $value)
        <option {{ $property->tipo_id == $value->id ? 'selected' : '' }}  value="{{ $value->id }}">{{$value->nome}}</option>
      @endforeach
    </select>
  </div>

I’m doing a check inside the option, so that if the Property tip_id is equal to the id of the type that is in the loop, it puts the Selected attribute.

I could not see the structure that your data is, but taking into account your comment this should work for you!

  • 1

    It worked, thank you.

Browser other questions tagged

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