如何通过'?filter[DBfieldname]=value&[DBfieldname2]=value2'在 Laravel 8 的 url 中
我需要根据获取请求来过滤数据,
当前路由
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
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 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>
I need to create below type of URL
http://127.0.0.1:8000/datasearch?filter[dbfieldname1]=searchAdmin&filter[dbfieldname2]=searchAgent
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要发出符合您要求的请求,只需使用此内容设置表单,例如
resources\views\home.blade.php
:配置路由
routes\web.php
:然后将搜索功能设置为
app\Http\Controllers\HomeController.php
:清除
storage\logs\laravel.log
,即可获得成功结果!To make request like your requirement, just set form with this content, example in
resources\views\home.blade.php
:Configure the route
routes\web.php
:Then set search function as
app\Http\Controllers\HomeController.php
:Clear
storage\logs\laravel.log
, then you will get successful result!只需提供一个包含路由参数键值的数组即可。例如:
Simply provide an array with key values to the route parameters. For example:
如果您不上传任何文件,您可能需要在表单中使用
enctype="application/x-www-form-urlencoded"
。multipart/form-data
将与需要文件上传的(特殊)POST
请求一起使用(我不确定当您将它与GET,它可能会被忽略)。然后,您必须记住,inputs/selects/textareas 上的每个
name="..."
属性将确定 URL 中数据的键,即您也可以更改此名称到
username[]
自动将输入解析为数组(我相信这一切都可以在 PHP 中开箱即用,最终取决于您的服务器),即最后,当您放置内容时在括号内,它将被解释为其中的键像这样的值:
If you're not uploading any files, you probably want to use
enctype="application/x-www-form-urlencoded"
with your forms. Themultipart/form-data
is to be used with (special)POST
requests for which you need file uploads (I'm not sure what happens when you use it with a GET, it might just get ignored).Then, you have to keep in mind that every
name="..."
attribute on your inputs/selects/textareas will determine the key of the data in the URL, i.e.You can also change this name to
username[]
to automatically have the inputs parsed as an array (I believe this all works out of the box with PHP, in the end it depends on your server), i.e.Lastly, when you put stuff inside the brackets, it will be interpreted as keys inside of that value like so: