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..
This is the output of $request->all()
array:2 [
  "type" => "customer"
  "users" => array:1 [
      0 => array:3 [
        "name" => "eeee"
        "username" => "eeee"
        "password" => "123456"
      ]
  ]
]
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.
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,
]);
You can also replace whole input with new one with request replace method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With