首页 > 解决方案 > 如何向控制器发送多个 id

问题描述

我想将我的 id 发送到控制器,但我的 id 被逗号分隔212,233,121,例如来自 JavaScript。如何根据我的基于 JavaScript 的数字逗号拆分的值发送我的 id?

url 应该是这样http://localhost/fics/public/reporting/reporting/getcust/212/233/121的,它是后面的数字,基于我用逗号分隔的值。

这是我的代码:

for (var h = 0; h < storeItem.length; h++)
{
    if (l == storeItem[h]) {
        popVal = popVal + tempItem[i][l];
        //popval value from my other window .Example " 002,212,212"
    }
}

我的查看路线按钮onclick:

onclick="openNewWindow('{{ route('reporting.getcust') }}')"

我的 web.php:

Route::group(['prefix' => 'reporting'], function () {
    Route::get('reporting/getcust/{id}','GenReportController@getcust')->name('reporting.getcust');
});

控制器:

public function getcust(Request $request){  
    $id = $_GET['id'];
}

标签: javascriptphplaravel

解决方案


如果 JavaScript 生成类似http://localhost/fics/public/reporting/reporting/getcust/002,212,212的 URL 。然后您可以使用该explode方法获取控制器中的所有ID

public function getcust(Request $request)
{ 
    $ids = explode(",", $request->id);

    foreach ($ids as $id) 
    {
        echo $id;
    }
}

推荐阅读