Reputation: 195
I am making a page with select option. If I select one option and click a button. It will go to that page with parameter in url.
I want to know is there any easy way to insert categoryId as parameter into action?
<form method="GET" action="{{ route('routename', ['categoryId' => 1]) }}">
@foreach($categories as $category)
<select>
<option value="1">{{ $category->name }}</option>
<option value="2">{{ $category->name }}</option>
<option value="3">{{ $category->name }}</option>
</select>
@endforeach
<button>Change</button>
</form>
Upvotes: 0
Views: 3950
Reputation: 1773
You should remove the categoryId from the route. Instead you should point your route to a controller that just accepts a Request. then you can read the value of the select box in the controller and direct the user to the view depending on the selected option.
Your view:
<form method="GET" action="{{ route('routename') }}">
@csrf
<select name='selectOption'>
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
</select>
@endforeach
<button>Change</button>
</form>
Your Route:
Route::get('url', 'Controller@functionName')->name('routename');
Your Controller:
use Illuminate\Http\Request;
public function functionName(Request $request)
{
$option = $request->selectOption;
if($option == 1){
CODE FOR OPTION 1 HERE
}
}
This way the route will not require the parameter to be defined in the URL. Instead you can pass it as a parameter in the request to be read in the controller. You could then have a if rule for each option that then returns a different view.
Upvotes: 2