如何通过'?filter[DBfieldname]=value&[DBfieldname2]=value2'在 Laravel 8 的 url 中

发布于 2025-01-11 22:54:09 字数 1121 浏览 5 评论 0原文

我需要根据获取请求来过滤数据,

当前路由

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箹锭⒈辈孓 2025-01-18 22:54:09

要发出符合您要求的请求,只需使用此内容设置表单,例如 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.  

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 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>

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.  
许仙没带伞 2025-01-18 22:54:09

只需提供一个包含路由参数键值的数组即可。例如:

route('products.index', ['manufacturer' => 'Samsung','price' => '10000']);

Simply provide an array with key values to the route parameters. For example:

route('products.index', ['manufacturer' => 'Samsung','price' => '10000']);
影子是时光的心 2025-01-18 22:54:09

如果您不上传任何文件,您可能需要在表单中使用 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']]

If you're not uploading any files, you probably want to use enctype="application/x-www-form-urlencoded" with your forms. The multipart/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.

<input type="text" name="username" value="foobar" />
// example.com?username=foobar
// Laravel will interpret it as ['username' => 'foobar']

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.

<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']]

Lastly, when you put stuff inside the brackets, it will be interpreted as keys inside of that value like so:

<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']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文