First we will define a route in the file web.php
that returns the data of the selected developer, and we will filter the desired fields with the only
method.
Route::get('developer/{developer}', function (App\Developer $developer) {
return $developer->only('name', '...');
});
Now we create the logic of select
, so that it requests the data to the route that we have created and establishes them in the inputs
desired. We will use the value
of select
to set the id
of the developer.
Vista blade.
<select id="developers">
@foreach($developers as $developer)
<option value="{{ $developer->id }}">{{ $developer->name }}</option>
@endforeach
</select>
Script for the select.
$('#developers').on('change', function() {
// Usaremos el método 'get' para obtener los
// datos del desarrollador mediante ajax.
$.get(encodeURI('developer/'+ this.value), function(developer) {
// Con el método 'each' recorremos los datos.
$.each(developer, function(key, data) {
// Buscamos un input que tenga el mismo nombre
// que nuestro campo, y establecemos su valor
// con los datos del desarrollador.
$("input[name="+ key +"]").val(data);
});
});
})