1

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.

Paulie-C
  • 1,674
  • 1
  • 13
  • 29
Brian Ruchiadi
  • 331
  • 1
  • 13
  • 29

1 Answers1

3

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.

arbogastes
  • 1,308
  • 9
  • 10