首页 > 解决方案 > 如何制作变量“?” 在 laravel 5.8 中

问题描述

我想制作如下参数

例如

http://localhost/firecek_web/pengaduan?barcode=8571385

路线

Route::get('/pengaduan','PengaduanController@index');

标签: phplaravellaravel-5request

解决方案


您无需更改路线定义。保持原样:

Route::get('/pengaduan', 'PengaduanController@index');

然后,在发出请求时将其附加到您的 url(就像您在描述中那样):

http://localhost/firecek_web/pengaduan?barcode=8571385
                                      ^^^^^^^^^^^^^^^^

然后在你的控制器中:

PengaduanController.php

public function index(Request $request)
{
    $value = $request->query('barcode');
    // this also works:
    $value = $request->get('barcode');
    // or even this:
    $value = $request->barcode;

    dd($value); // '8571385'
}

检查文档的这一部分

从查询字符串中检索输入

虽然该input方法从整个请求负载(包括查询字符串)中检索值,但该query方法只会从查询字符串中检索值:

$name = $request->query('name');

如果请求的查询字符串值数据不存在,则返回此方法的第二个参数:

$name = $request->query('name', 'Helen');

您可以query不带任何参数调用该方法,以便将所有查询字符串值作为关联数组检索:

$query = $request->query();

推荐阅读