如何从 Laravel 表单请求中获取密钥?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40807075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:48:56  来源:igfitidea点击:

How to get the key from a laravel form request?

phplaravellaravel-5

提问by Rubberduck1337106092

I have an array

我有一个数组

array:2 [▼
  2 => "12"
  7 => "12"
]

Send from a form, I need the 2 and 7 how do i call them?

从表格发送,我需要 2 和 7 我该如何称呼它们?

The keys are id's of parts so want to. foreach get the ID and value then update something...

键是部件的 id,所以想要。foreach 获取 ID 和值,然后更新某些内容...

foreach($request->except('_token') as $part) {
    /*get Key here (in this case 2 or 7) and get value here (in this case both 12)*/
}

Can somebody tell me how to do this?

有人能告诉我怎么做吗?

Thanks in advance.

提前致谢。

回答by Praveen Kumar Purushothaman

Use $key => $valuenotation in foreach:

在 中使用$key => $value符号foreach

foreach ($request->except('_token') as $key => $part) {
  // $key gives you the key. 2 and 7 in your case.
}

回答by Md. Abu Taleb

$data = $request->except('_token')
foreach($data as $id => $value){
    echo "My id is ". $id . " And My value is ". $value;
}

回答by Aleksandar

If You want to access exactly one $requestkey, You can use:

如果您只想访问一个$request密钥,您可以使用:

$request->key_name

to achieve this.

为达到这个。

Example

例子

I was sending an imagein postman like this: Postman image upload example

我正在发送这样image的邮递员: 邮递员图片上传示例

And I got it in Laravel's controller like this:

我在 Laravel 的控制器中得到了它,如下所示:

public function store(SendMessageRequest $request)
{
    $image = $request->image; // this is what You need :)
    // ...
}