Hi,
In my form on VILT stack, I have a watcher for country selection, when I choose a country, it gets provinces of its:
//On Place/Edit page:
watch(country_id, throttle((newCountry_id) => {
router.visit(route('admin.places.partial_data.get_provinces'), {
method: 'post',
data: {page_path:'Places/Edit', country_id: newCountry_id},
only: ['provinces'],
replace: true,
preserveState: true,
preserveScroll: true,
})
}, 500))
and back-end:
//Controller:
public function getProvinces(Request $request)
{
$validated = $request->validate([
'country_id' => 'required|max:36',
'page_path' => [
'required',
Rule::in(['Places/Edit', 'Places/Create']),
],
]);
$provinces = Place::provinces($validated['country_id'])->get();
return Inertia::render('Admin/'.$validated['page_path'], [
'provinces' => $provinces,
]);
}
//Route:
Route::prefix('partial_data')->name('partial_data.')->group(function () {
Route::post('/get_provinces', [PlaceController::class, 'getProvinces'])->name('get_provinces');
});
I don't want repeat my self and make two getProvinces method for Create part and Edit part so I send page_path and send back response to where it comes.
It works fine. only problem is when I choose country my URL change to "/admin/places/partial_data/get_provinces". page is same and works correctly but url changed.
Actually, when we send data to a url with POST for getting some data back, Why URL should change?! It's not make sence!
what I should to do for preventing changing url?
Second question:
When you want make just one method to send some data to two different pages, are you agree to get partial data like that? How you code in same situation?
Thank you.