如何使用合并替换Laravel请求中的嵌套数组值? [英] How to replace nested array value inside the Laravel request using merge?
本文介绍了如何使用合并替换Laravel请求中的嵌套数组值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个函数可以替换请求中的输入,这称为合并.我想更改嵌套数组的值,以便可以通过 $ this-> validate方法
.. p进行验证.
There is a function to replace the input from the request which is called merge.
I would like to change a value of a nested array so that it can be validated by $this->validate method
..
这是 $ request-> all()
array:2 [
"type" => "customer"
"users" => array:1 [
0 => array:3 [
"name" => "eeee"
"username" => "eeee"
"password" => "123456"
]
]
]
如果使用forloop,如何访问和更改用户名
How do I access the username value and change it, provided I use a forloop
for($i=0; $i < count($request->users); $i++){
// i need to access the value here
// i have done something like $request->merge(['users'][$index]['username'] => 'xxx');
// it doesnt work
}
有解决方案的人吗?谢谢.
Any solution guys? Thank you.
推荐答案
您可以使用merge方法尝试以下操作:
You can try something like this using merge method:
$new_users_data = $request->input('users');
foreach ($new_user_data as &$user_data) {
$user_data['username'] = 'new name';
}
$request->merge([
'users' => $new_users_data,
]);
您还可以使用请求替换方法将整个输入替换为新输入.
这篇关于如何使用合并替换Laravel请求中的嵌套数组值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文