firstplanb's avatar

Dependent Dropdowns, Options Not Returned

Hello! I'm trying to create multi-level dependent dropdowns using this plugin: https://github.com/kartik-v/dependent-dropdown but the options for the second selection are not showing up. Here is my code:

The View

<div class="form-group col-md-4">
    <label for="country_name">Country</label>
    <select class="form-control" name="country_name" id="countrySelector{{ $memberprofile->id }}">
        <option></option>
            @foreach( $countries as $country)
                <option value="{{ $country->id }}">{{ $country->name }}</option>
            @endforeach
    </select>
</div>
<div class="form-group col-md-4">
    <label for="region_name">Region</label>
    <select class="form-control" name="region_name" id="regionSelector{{ $memberprofile->id }}">
    </select>
</div>

JQuery script

// Region Selector
$("#regionSelector{{ $memberprofile->id }}").depdrop({
    depends: ['countrySelector{{ $memberprofile->id }}'],
    url: '{{ URL::route('regionselect') }}'        
});

Route

Route::get('/regionselect', ['as' => 'regionselect', 'uses' => 'User\MemberProfileFormController@regionSelector']);

Controller

public function regionSelector(Request $request)
{
    $childregions = ""; // define output variable   
    $countryname = 'Philippines';
    $country = Country::where('name','=', $countryname)->first();
    $countryid = $country->id;
            
    $regions = Region::where('country_id', '=', $countryid)->get();
                    
    return Response::json(['output'=>$regions, 'selected'=>'']);
}

When I load the route {{ URL::route('regionselect') }} in the browser, the region options are being returned correctly but in the actual form the regions are not returned. It just says "Loading...".

How do I properly return the options for the region selection? Thank you for the help.

0 likes
5 replies
firstplanb's avatar

Any insight on this is very much appreciated.

compmeist's avatar

Perhaps examine the network request in the browser dev panel ?

olimaz's avatar

The problem is in your routes, should be post, not get

Snapey's avatar

From the docs;

output is an array of data for the dependent list of the format {id: <option-value>, name: <option-name>}

What are you returning? I note you just use ->get(); so you are pulling the full model.

You probably instead need to use pluck and return only id and name columns

Use browser network tools to see what is being returned so that you can get the query right.

Snapey's avatar

@firstplanb please ignore my post. I did not realise that @olimaz had pointlessly posted inaccurate information on a 5 month old thread

Jeez!

Please or to participate in this conversation.