Jorge Ivan Aguirre
Jorge Ivan Aguirre

Reputation: 129

Select is not gettion the value of the option

I am working in laravel and I am passing a model into a view and I am showing one of the attributes of the model in every option but I need that the value is another one, something like showing the name of a model but I need to save the id but it it getting all the model. the code is:

<div class="form-group">
  <div class="col-sm-12">
     <label for="select2">Persona</label>
       <select name="persona" id="persona" class="select2" required>
          @foreach ($personas as $persona)
             <option value="{{$persona->persona_id}}">{{$persona-
              >getNombreApellidoPersona()}}</option>
          @endforeach
       </select>
   </div>
</div>

the method getNombrePersona() is working because is returning the name and the lastname from the table, but when I return what the request give me, this what it is return in the row persona:

 "tipo_usuario" => "{"tipo_usuario_id":2,"cliente":1,"nombre":"admin finca","fecha_insert":"2017-04-20 16:58:24","fecha_update":"2017-04-20 16:58:24","eliminado":0}"

And in the chrome when I check the source code in that part this is what it is shown:

<option value="{&quot;persona_id&quot;:1,&quot;nombre&quot;:&quot;juan&quot;,&quot;apellido&quot;:&quot;aguirre&quot;,&quot;correo_electronico&quot;:null,&quot;fecha_insert&quot;:&quot;2017-04-17 20:42:16&quot;,&quot;fecha_update&quot;:&quot;2017-04-17 20:42:16&quot;,&quot;eliminado&quot;:0}">name last name</option>

It is like if the value that I give is replace by the whole information if the model.

I do not know why this is happening, I know that I can simply take the "persona_id" from the whole model but I want to know if it is possible to take directly from the select.

Upvotes: 2

Views: 31

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

Change loop part to this:

<option value="{{ $persona->persona_id }}">{!! $persona->getNombreApellidoPersona() !!}</option>

By default, Blade {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Hello, {!! $name !!}

https://laravel.com/docs/5.4/blade#displaying-data

Upvotes: 1

Related Questions