fix: load cities dynamically based on selected province
This commit is contained in:
@@ -8,12 +8,14 @@ use Illuminate\Http\JsonResponse;
|
|||||||
|
|
||||||
class LocationController extends Controller
|
class LocationController extends Controller
|
||||||
{
|
{
|
||||||
public function getCities($provinceId): JsonResponse
|
public function getCities(int $provinceId): JsonResponse
|
||||||
{
|
{
|
||||||
$cities = City::where('province_id', $provinceId)
|
$cities = City::query()
|
||||||
->orderBy('name')
|
->where('province_id', $provinceId)
|
||||||
|
->orderBy('name', 'asc')
|
||||||
->get(['id', 'name']);
|
->get(['id', 'name']);
|
||||||
|
|
||||||
|
|
||||||
return response()->json($cities);
|
return response()->json($cities);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,12 +97,12 @@ class RegisterPostRequest extends FormRequest
|
|||||||
'alpha_num',
|
'alpha_num',
|
||||||
'confirmed'
|
'confirmed'
|
||||||
],
|
],
|
||||||
'user_province' => [
|
'province_id' => [
|
||||||
'nullable',
|
'nullable',
|
||||||
'integer',
|
'integer',
|
||||||
'exists:provinces,id'
|
'exists:provinces,id'
|
||||||
],
|
],
|
||||||
'user_city' => [
|
'city_id' => [
|
||||||
'nullable',
|
'nullable',
|
||||||
'integer',
|
'integer',
|
||||||
'exists:cities,id'
|
'exists:cities,id'
|
||||||
|
|||||||
@@ -271,37 +271,68 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
</main>
|
</main>
|
||||||
|
@push('scripts')
|
||||||
|
<script>
|
||||||
|
$(document).ready(function () {
|
||||||
|
const provinceSelect = $('#province_id');
|
||||||
|
const citySelect = $('#city_id');
|
||||||
|
|
||||||
<script>
|
|
||||||
$(document).ready(function () {
|
|
||||||
$('#province_id').on('change', function () {
|
|
||||||
let provinceId = $(this).val();
|
|
||||||
let citySelect = $('#city_id');
|
|
||||||
|
|
||||||
citySelect.prop('disabled', true);
|
const oldProvinceId = "{{ old('province_id') }}";
|
||||||
|
const oldCityId = "{{ old('city_id') }}";
|
||||||
|
|
||||||
if (!provinceId) {
|
function loadCities(provinceId, selectedCityId = null) {
|
||||||
citySelect.html('<option value="">ابتدا استان را انتخاب کنید</option>');
|
if (!provinceId) {
|
||||||
return;
|
citySelect.html('<option value="">ابتدا استان را انتخاب کنید</option>');
|
||||||
|
citySelect.prop('disabled', true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
citySelect.html('<option value="">در حال بارگذاری شهرها...</option>');
|
||||||
|
citySelect.prop('disabled', true);
|
||||||
|
|
||||||
|
let url = "{{ route('locations.cities', ':provinceId') }}".replace(':provinceId', provinceId);
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: url,
|
||||||
|
type: 'GET',
|
||||||
|
dataType: 'json',
|
||||||
|
success: function (cities) {
|
||||||
|
citySelect.empty();
|
||||||
|
citySelect.append('<option value="">انتخاب شهر</option>');
|
||||||
|
|
||||||
|
$.each(cities, function (index, city) {
|
||||||
|
let isSelected = (selectedCityId && selectedCityId == city.id) ? 'selected' : '';
|
||||||
|
citySelect.append(
|
||||||
|
`<option value="${city.id}" ${isSelected}>${city.name}</option>`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
citySelect.prop('disabled', false);
|
||||||
|
},
|
||||||
|
error: function (xhr) {
|
||||||
|
console.error('Error:', xhr.responseText);
|
||||||
|
citySelect.html('<option value="">خطا در دریافت لیست شهرها</option>');
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
citySelect.html('<option value="">در حال بارگذاری...</option>');
|
|
||||||
|
|
||||||
$.get('/get-cities/' + provinceId, function (data) {
|
provinceSelect.on('change', function () {
|
||||||
citySelect.html('<option value="">انتخاب شهر</option>');
|
let provinceId = $(this).val();
|
||||||
|
loadCities(provinceId);
|
||||||
$.each(data, function (key, city) {
|
|
||||||
citySelect.append(
|
|
||||||
'<option value="' + city.id + '">' + city.name + '</option>'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
citySelect.prop('disabled', false);
|
|
||||||
}).fail(function () {
|
|
||||||
citySelect.html('<option value="">خطا در دریافت شهرها</option>');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
if (oldProvinceId) {
|
||||||
|
loadCities(oldProvinceId, oldCityId);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
</script>
|
||||||
</script>
|
@endpush
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@endsection
|
@endsection
|
||||||
|
|||||||
+6
-3
@@ -3,11 +3,14 @@
|
|||||||
use App\Http\Controllers\Auth\LoginController;
|
use App\Http\Controllers\Auth\LoginController;
|
||||||
use App\Http\Controllers\Auth\LogoutController;
|
use App\Http\Controllers\Auth\LogoutController;
|
||||||
use App\Http\Controllers\Auth\RegisterController;
|
use App\Http\Controllers\Auth\RegisterController;
|
||||||
|
use App\Http\Controllers\LocationController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
Route::prefix('locations')->name('locations.')->controller(\App\Http\Controllers\LocationController::class)->group(function () {
|
Route::prefix('locations')->name('locations.')
|
||||||
Route::get('/cities/{provinceId}', 'getCities')->name('cities');
|
->controller(LocationController::class)
|
||||||
});
|
->group(function () {
|
||||||
|
Route::get('/cities/{provinceId}', 'getCities')->name('cities');
|
||||||
|
});
|
||||||
|
|
||||||
Route::prefix('auth')->name('auth.')->group(function () {
|
Route::prefix('auth')->name('auth.')->group(function () {
|
||||||
Route::middleware('guest:web')->group(function () {
|
Route::middleware('guest:web')->group(function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user