Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Khalid475's avatar

dependent dropdown list

iam getting the error in console :

jquery.min.js:5722 
 GET http://127.0.0.1:8000/ShippingPrices/1 404 (Not Found)
send	@	jquery.min.js:5722
ajax	@	jquery.min.js:5383
(anonymous)	@	ShippingPrices:503
dispatch	@	jquery.min.js:2763
v.handle	@	jquery.min.js:2647
handleMouseUp_	@	unknown

i would like to get Cities belong to selected Country and i have tried the following code: in Controller:

    public function GetCities($CountryID)
    {
        $cities = City::where("CountryID", $CountryID)->orderby("CityName", 'CityID')->get();
        return json_encode($cities);
    }

Blade:

<div class="col-12">
                                    <label class="form-label"
                                        for="CountryName">{{ trans('shippingprices.CountryName') }}</label>
                                    <div class="mb-1">
                                        <select class="select2 form-select" id="CountryID" name="CountryID">
                                            <option selected disabled>{{ trans('shippingprices.Choose') }}...</option>
                                            @foreach ($countries as $countries)
                                                <option value="{{ $countries->id }}">{{ $countries->CountryName }}
                                                </option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                                <div class="col-12">
                                    <label class="form-label"
                                        for="CityName">{{ trans('shippingprices.CityName') }}</label>
                                    <div class="mb-1">
                                        <select class="select2 form-select" id="CityID" name="CityID">
                                        </select>
                                    </div>
                                </div>
        <script src="{{ asset('assets/common-files/js/jquery.min.js') }}"></script>
        <script type="text/javascript">
            $(document).on('change', '#CountryID', function() {
                var CountryID = $(this).val();
                if (CountryID) {
                    $.ajax({
                        url: "{{ url('/ShippingPrices') }}/" + CountryID,
                        type: "GET",
                        dataType: "json",
                        success: function(data) {
                            $('select[name="CityID"]').html('');
                            var d = $('select[name="CityID"]').empty();
                            $.each(data, function(key, value) {
                                $('select[name="CityID"]').append(
                                    '<option value="' + value.CityID + '">' + value
                                    .CityName + '</option>');
                            });
                        },
                    });
                } else {
                    alert('danger');
                }
            });
        </script>

Route:

Route::resource('/ShippingPrices', ShippingPriceController::class);

Thanks

0 likes
10 replies
SilenceBringer's avatar

@khalid475 add the route

Route::get('/ShippingPrices/{country_id}', [YourController::class, 'GetCities']);

Route::resource do not include your custom routes, just standard CRUD routes

Khalid475's avatar

@SilenceBringer Thank you but still the same error is coming i added the route:

Route::get('/ShippingPrices/{id}', [ShippingPriceController::class, 'GetCities']);

and added in ajax url:

        <script src="{{ asset('assets/common-files/js/jquery.min.js') }}"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $('select[name="CountryID"]').on('change', function() {
                    var CountryID = $(this).val();
                    if (CountryID) {
                        $.ajax({
                            url: "{{ url('/ShippingPrices/') }}/" + CountryID,
                            type: "GET",
                            dataType: "json",
                            success: function(data) {
                                $('select[name="CityID"]').html('');
                                var d = $('select[name="CityID"]').empty();
                                $.each(data, function(key, value) {
                                    $('select[name="CityID"]').append(
                                        '<option value="' + value.CityID + '">' + value
                                        .CityName + '</option>');
                                });
                            },
                        });
                    } else {
                        alert('danger');
                    }
                });
            });
        </script>

and the controller is the same

    public function GetCities($CountryID)
    {
        $cities = City::where("CountryID", $CountryID)->orderby("CityName", 'CityID')->get();
        return json_encode($cities);
    }
Khalid475's avatar

@SilenceBringer yes its in Group

Route::group(
    [
        'prefix' => LaravelLocalization::setLocale(),
        'middleware' => ['localeSessionRedirect', 'localizationRedirect', 'localeViewPath'],
    ], function () {

        Route::group(['prefix' => 'admin'], function () {
            Route::group(['middleware' => 'admin.guest'], function () {
                Route::view('/', 'admin.auth.login')->name('admin.auth.login');
                Route::post('/', [AdminController::class, 'authenticate'])->name('admin.auth');
            });

            Route::group(['middleware' => 'admin.auth'], function () {
                Route::get('/dashboard', [DashboardController::class, 'dashboard'])->name('admin.content.dashboard.dashboard');
                Route::get('/logout', [AdminController::class, 'logout'])->name('admin.logout');

                ///////////////////////////////////////////// Countries ///////////////////////////////////////////////
                Route::resource('/Countries', CountryController::class);

                ///////////////////////////////////////////// Cities ///////////////////////////////////////////////
                Route::resource('/Cities', CityController::class);

                Route::resource('/ShippingPrices', ShippingPriceController::class);
                Route::get('/ShippingPrices/{CountryID}', [ShippingPriceController::class, 'GetCities']);

            });

        });
    });
SilenceBringer's avatar

@Khalid475 this way your route

url: "{{ url('/ShippingPrices/') }}/" + CountryID,

must be prefixed by locale and admin, no? Something like

url: "{{ url('/en/admin/ShippingPrices/') }}/" + CountryID,
Khalid475's avatar

i forget the Semicolon and i try it gives me the same error

GET http://127.0.0.1:8000/en/admin/ShippingPrices/1 404 (Not Found)
Khalid475's avatar
Khalid475
OP
Best Answer
Level 1

Issue Solved by creating new route file thanks you

Please or to participate in this conversation.