Bring data through a Laravel select

0

I am doing a project where, by means of a select I choose a developer and I bring the data from the database of said example developer (I select developer Daniel and bring me his specialty and hours) I tried to do it but the only thing I could do is to bring the developers to the select and setee the name to a input . Could you tell me how I can do to bring that data and show it in input ?

Sight code

    
asked by DANIEL FELIPE LOPEZ VARGAS 09.11.2017 в 05:07
source

2 answers

1

You only have to do a service to which the developer id gives you the data you want, and in the view by javascript / jquery set it

$("#select").onchange(function(){
    $.ajax({url: "/test.php?id=".$(this).attr("id"), success: function(result){
        $("#input_1").val(result.precio);
        $("#input_2").html(result.especialidad);
    }});
});
    
answered by 09.11.2017 в 09:34
0

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);
        });
    });
})
    
answered by 09.11.2017 в 21:13