如何修复搜索项目中国家-城市动态下拉框无法加载对应城市的问题
Let's walk through the issues in your code and fix them step by step—looks like the main problems are model mismatches, missing controller logic, and routing gaps that are blocking city data from loading.
1. Correct the Cities Model
First, your Cities model has a critical typo in its method, and we need to add logic to fetch cities linked to a specific location. I assume your newcitymun table has a location_id foreign key connecting it to the location table:
namespace App; use Illuminate\Database\Eloquent\Model; class Cities extends Model { protected $table = 'newcitymun'; protected $fillable = ['citymunDesc', 'location_id']; // Include the foreign key here public $timestamps = false; // Fetch cities filtered by location ID public static function getCitiesByLocation($locationId) { return self::where('location_id', $locationId)->orderBy('citymunDesc')->get(); } }
2. Add a Controller to Handle AJAX Requests
You're missing a controller method to process the ajax_citymun request. Create or update a LocationController with this logic:
namespace App\Http\Controllers; use App\Cities; use Illuminate\Http\Request; class LocationController extends Controller { // Handle AJAX call to get cities for a selected location public function getCitiesByLocation($locationId) { $citymuns = Cities::getCitiesByLocation($locationId); return view('partials.citymun', compact('citymuns')); } }
3. Define the Missing Route
Add this route to routes/web.php to map your AJAX URL to the controller method:
Route::get('ajax_citymun/{id}', [App\Http\Controllers\LocationController::class, 'getCitiesByLocation']);
4. Fix the Initial City Dropdown in Your View
Your index view is calling a non-existent Citymun model (your model is named Cities). Also, we should leave the city dropdown empty initially since it should populate dynamically:
<select id="location" name="location" class="form-control"> <option value="">{{ __('messages.select_location') }}</option> @foreach(\App\Location::orderBy('location_name')->get() as $location) <option value="{{$location->id}}">{{$location->location_name}}</option> @endforeach </select> <select id="citymun" name="citymun" class="form-control"> <option value="">{{ __('messages.select_city') }}</option> <!-- Update the placeholder text --> </select>
5. Fix the City Partial View
Your citymun.php partial has a variable name collision (you're reusing $citymun for both the collection and individual items). Update it to:
<option value="">.. select city ..</option> @foreach($citymuns as $citymun) <!-- Use $citymuns for the collection --> <option value="{{$citymun->id}}">{{$citymun->citymunDesc}}</option> @endforeach
6. Improve the AJAX JavaScript
Add an error handler to debug issues and clean up the code a bit:
<script type="text/javascript"> $(document).ready(function(e) { $("#location").change(function(){ var loc = $(this).val(); // Use $(this) instead of re-selecting the element if(loc) { $.ajax({ type: "GET", url: "{{ url('ajax_citymun') }}/" + loc, success: function(result){ $("#citymun").html(result); }, error: function(xhr) { console.log(xhr.responseText); // Log errors to browser console for debugging } }); } else { // Reset city dropdown if no location is selected $("#citymun").html('<option value="">{{ __('messages.select_city') }}</option>'); } }); }); </script>
Quick Checks Before Testing:
- Confirm your
newcitymuntable has alocation_idcolumn that links to theidcolumn in thelocationtable (this is how we filter cities by country). - Clear Laravel's cache with
php artisan cache:clearandphp artisan view:clearto ensure your changes take effect.
内容的提问来源于stack exchange,提问作者John Arzaga

