如何在 Laravel 8 中的 url 中传递 '?filter[DB fieldname]=value[field name2]=value2'
How pass '?filter[DBfieldname]=value&[DBfieldname2]=value2' in url in Laravel 8
我需要根据获取的请求过滤数据,
当前路线
Route::get('datasearch', [Mycontroller::class, 'MyFunction'])->name('this.is.route.name');
当前 Forntend 形式
<form method="get" enctype="multipart/form-data" action="{{ route('this.is.route.name') }}">
@csrf
<select class="form-control" name="searchAdmin">
<option class="hidden" selected disabled>Admin List </option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
</select>
<<select class="form-control" name="searchAgent">
<option class="hidden" 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>
我需要创建以下类型的 URL
http://127.0.0.1:8000/datasearch?filter[dbfieldname1]=searchAdmin&filter[dbfieldname2]=searchAgent
只需为路由参数提供一个包含键值的数组。例如:
route('products.index', ['manufacturer' => 'Samsung','price' => '10000']);
要像您的要求一样提出请求,只需使用此内容设置表格,例如 resources\views\home.blade.php
:
<form method="get" enctype="multipart/form-data" action="{{ route('search.data') }}">
<select class="form-control" name="filter[dbfieldname1]">
<option class="hidden" disabled selected>Admin List </option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
</select>
<select class="form-control" name="filter[dbfieldname2]">
<option class="hidden" 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>
配置路由routes\web.php
:
Route::get('/search/data', [App\Http\Controllers\HomeController::class, 'search'])->name('search.data');
然后设置搜索功能为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!');
}
}
清除storage\logs\laravel.log
,即可获得成功结果!
[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.
如果您不上传任何文件,您可能希望对您的表单使用 enctype="application/x-www-form-urlencoded"
。 multipart/form-data
将与需要文件上传的(特殊)POST
请求一起使用(我不确定将它与 GET 一起使用时会发生什么,它可能会被忽略)。
然后,您必须记住,inputs/selects/textareas 上的每个 name="..."
属性都将确定 URL 中数据的键,即
<input type="text" name="username" value="foobar" />
// example.com?username=foobar
// Laravel will interpret it as ['username' => 'foobar']
您也可以将此名称更改为 username[]
以自动将输入解析为数组(我相信这一切都可以直接使用 PHP,最终取决于您的服务器),即
<input type="text" name="username[]" value="foo" />
<input type="text" name="username[]" value="bar" />
// example.com?username[]=foo&username[]=bar
// Laravel will interpret it as ['username' => ['foo', 'bar']]
最后,当您将内容放在括号内时,它将被解释为该值内的键,如下所示:
<input type="text" name="username[a]" value="foo" />
<input type="text" name="username[b]" value="bar" />
// example.com?username[a]=foo&username[b]=bar
// Laravel will interpret it as ['username' => ['a' => 'foo', 'b' => 'bar']]
我需要根据获取的请求过滤数据,
当前路线
Route::get('datasearch', [Mycontroller::class, 'MyFunction'])->name('this.is.route.name');
当前 Forntend 形式
<form method="get" enctype="multipart/form-data" action="{{ route('this.is.route.name') }}">
@csrf
<select class="form-control" name="searchAdmin">
<option class="hidden" selected disabled>Admin List </option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
</select>
<<select class="form-control" name="searchAgent">
<option class="hidden" 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>
我需要创建以下类型的 URL
http://127.0.0.1:8000/datasearch?filter[dbfieldname1]=searchAdmin&filter[dbfieldname2]=searchAgent
只需为路由参数提供一个包含键值的数组。例如:
route('products.index', ['manufacturer' => 'Samsung','price' => '10000']);
要像您的要求一样提出请求,只需使用此内容设置表格,例如 resources\views\home.blade.php
:
<form method="get" enctype="multipart/form-data" action="{{ route('search.data') }}">
<select class="form-control" name="filter[dbfieldname1]">
<option class="hidden" disabled selected>Admin List </option>
<option value="1">Value 1</option>
<option value="2">Value 2</option>
</select>
<select class="form-control" name="filter[dbfieldname2]">
<option class="hidden" 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>
配置路由routes\web.php
:
Route::get('/search/data', [App\Http\Controllers\HomeController::class, 'search'])->name('search.data');
然后设置搜索功能为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!');
}
}
清除storage\logs\laravel.log
,即可获得成功结果!
[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.
如果您不上传任何文件,您可能希望对您的表单使用 enctype="application/x-www-form-urlencoded"
。 multipart/form-data
将与需要文件上传的(特殊)POST
请求一起使用(我不确定将它与 GET 一起使用时会发生什么,它可能会被忽略)。
然后,您必须记住,inputs/selects/textareas 上的每个 name="..."
属性都将确定 URL 中数据的键,即
<input type="text" name="username" value="foobar" />
// example.com?username=foobar
// Laravel will interpret it as ['username' => 'foobar']
您也可以将此名称更改为 username[]
以自动将输入解析为数组(我相信这一切都可以直接使用 PHP,最终取决于您的服务器),即
<input type="text" name="username[]" value="foo" />
<input type="text" name="username[]" value="bar" />
// example.com?username[]=foo&username[]=bar
// Laravel will interpret it as ['username' => ['foo', 'bar']]
最后,当您将内容放在括号内时,它将被解释为该值内的键,如下所示:
<input type="text" name="username[a]" value="foo" />
<input type="text" name="username[b]" value="bar" />
// example.com?username[a]=foo&username[b]=bar
// Laravel will interpret it as ['username' => ['a' => 'foo', 'b' => 'bar']]