Skip to content
Advertisement

How to get “city” field from Clients table in laravel query properly?

public function FindDomainDependCityClient(Request $request) {
         $city = Client::select('city')
              ->where('clients.id', '=', $request->id)
              ->get();

the query above doesn’t get any result, otherwise if i replace ($request->id) with ‘casablanca’ (the city) in mysql phpMyAdmin I get the result

    $data = Domaine::select('id_domain', 'nom_domain')
            ->where('Domaines.city', $city)
            ->get();

    return response()->json($data);

I use Jquery & Ajax to get the data

$(document).on('change', '#id', function() {
    var id = $(this).val();

    $.ajax({
        type: 'get',
        url: '{!! URL::to('/finddomaindependcityclient') !!}',
        data: {'id': id},
        success: function(data) {
            console.log('success !!');
            console.log(data);
            console.log(data.length);

            var fillDropDown = '<option selected disabled>Sélectionner le domaine</option>';
            for (var i = 0; i < data.length; i++) {
                fillDropDown += '<option value="'+ data[i].id_domain + '">' + data[i].nom_domain + '</option>';
            }
            $('#id_dom').html(""); //clear input values
            $('#id_dom').append(fillDropDown);
        },
        error: function(msg) {
            console.log('error getting data !!');
        }
    });
});

Advertisement

Answer

you need to get city name from Client model. as you are using get() that gives you a collection and your next query is not working. So do it like

$city = Client::find($request->id)->city;

and then get the domains related to the city

$data = Domaine::select('id_domain', 'nom_domain')
            ->where('city', $city)
            ->get();
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement