如何从 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
How to get the key from a laravel form request?
提问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
回答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 $request
key, You can use:
如果您只想访问一个$request
密钥,您可以使用:
$request->key_name
to achieve this.
为达到这个。
Example
例子
I was sending an image
in postman like this:
And I got it in Laravel's controller like this:
我在 Laravel 的控制器中得到了它,如下所示:
public function store(SendMessageRequest $request)
{
$image = $request->image; // this is what You need :)
// ...
}