I need for filtering data based on getting requests,
Current Route
Route::get('datasearch', [Mycontroller::class, 'MyFunction'])->name('this.is.route.name');
Current Forntend form
<form method="get" enctype="multipart/form-data" action="{{ route('this.is.route.name') }}">
@csrf
<select name="searchAdmin">
<option selected disabled>Admin List </option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
</select>
<<select name="searchAgent">
<option selected disabled>Agent List </option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
</select>
<input type="submit" value="Search Data" />
</form>
I need to create below type of URL
http://127.0.0.1:8000/datasearch?filter[dbfieldname1]=searchAdmin&filter[dbfieldname2]=searchAgent
CodePudding user response:
Simply provide an array with key values to the route parameters. For example:
route('products.index', ['manufacturer' => 'Samsung','price' => '10000']);
CodePudding user response:
To make request like your requirement, just set form with this content, example in resources\views\home.blade.php
:
<form method="get" enctype="multipart/form-data" action="{{ route('search.data') }}">
<select name="filter[dbfieldname1]">
<option disabled selected>Admin List </option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
</select>
<select name="filter[dbfieldname2]">
<option disabled selected>Agent List </option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
</select>
@csrf
<input type="submit" value="Search Data" />
</form>
Configure the route routes\web.php
:
Route::get('/search/data', [App\Http\Controllers\HomeController::class, 'search'])->name('search.data');
Then set search function as app\Http\Controllers\HomeController.php
:
// Get search data
public function search(Request $request)
{
logger('Function search is working.');
$filter = $request->filter;
if($filter) {
if(isset($filter['dbfieldname1'])){
logger('Value of dbfieldname1:');
logger($filter['dbfieldname1']);
} else {
logger('Admin list is not choosen.');
}
if(isset($filter['dbfieldname2'])){
logger('Value of dbfieldname2:');
logger($filter['dbfieldname2']);
} else {
logger('Agent list is not choosen.');
}
} else {
logger('Not any lists choosen!');
}
}
Clear storage\logs\laravel.log
, then you will get successful result!
[2022-03-05 05:53:54] local.DEBUG: Function search is working.
[2022-03-05 05:53:54] local.DEBUG: Admin list is not choosen.
[2022-03-05 05:53:54] local.DEBUG: Value of dbfieldname2:
[2022-03-05 05:53:54] local.DEBUG: 2
[2022-03-05 05:54:24] local.DEBUG: Function search is working.
[2022-03-05 05:54:24] local.DEBUG: Value of dbfieldname1:
[2022-03-05 05:54:24] local.DEBUG: 1
[2022-03-05 05:54:24] local.DEBUG: Agent list is not choosen.