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

Wygekas's avatar

Dynamic form action with method GET

Im making car ads web part. User in form can choose what make he is looking for, model, year ..... So i need to assemble URL with selections which controller could process later. This is my blade:

<form action="{{route('ads_list.index') }}" method="GET">
	<label>Make</label>
	<select id="makes_select" name="make_id">
		<option>SAAB</option>
		<option>TRIUMPH</option>
		<option>LOTUS</option>
	</select>  

	<select id="year_select" name="year">
		<option>2000</option>
		<option>2001</option>
		<option>2002</option>
	</select> 
<button type="submit"> SUBMIT </button>
</form> 

This is my routes

Route::resource('localhost' ,App\Http\Controllers\Skelbimai_mainController::class); //here i choose filters
Route::resource('localhost/ads_list' ,App\Http\Controllers\Ads_listController::class); //here i want display ads according to filters

If i submit data i selected in form, I get redirected to intended route ads_list with all variables in URL (localhost/ads_list?make_id=SAAB&year=2000). BUT my resource controller doesn't return anything, it's just blank page (they should return("damn this works"). According to route:list route exist and is correct, just all those extra values to URL messes up something. If i simply add csrf and change method to POST i do get controller return value, but then URL won't have variables.

0 likes
4 replies
jlrdw's avatar

Normally you don't have localhost in the route.

estudiante_uap's avatar
public function index(Request $request)
{
	$make_id = $request->make_id;
	$year = $request->year;

	$query = Model::where('field', $make_id)->where('field', $year);

	return view('your_view', compact('query'));
}
Wygekas's avatar
Wygekas
OP
Best Answer
Level 1

I did kinda solve this.

<form action="localhost/ads_list/index" method="GET">

For some strange reasons it returns show method, not index. I tried some other variations, but this was the only one that worked. This way i can access at least one method of resource controller and have all variables in URL. Using localhost.ads_list.index or similar variations return 404 error.

Zigi84's avatar

This is because you are using the wrong helper. Route generates a uri for a named route, which you didn't name.

Use url helper instead. That way you don't have to type localhost, and it'll work on deployment.

Please or to participate in this conversation.