0

I have a Laravel Request object (collection) and there is an array inside this (address array) I want to add a item to this array. I tried $request->address['state'] = 'test'; and following error occurred.

Indirect modification of overloaded property Illuminate\Http\Request::$address has no effect

Here is the request collection

I want to add a item like highlighted in this img

3
  • did you tried this stackoverflow.com/questions/36812476/… Commented Mar 7, 2018 at 9:10
  • @vijaykumar yes, but the above question solutions use to change collection values, but I want to add item to an array inside a collection Commented Mar 7, 2018 at 9:15
  • 1
    Have you even read the documentation? This is super trivial Commented Mar 7, 2018 at 9:25

1 Answer 1

1

The easiest way it to get the associative array from the request and play with it.

$myRequest = $request->all();
$myRequest['address'] = ['state' => 'test'];

otherwise, you have to modify the request object you need to add this code:

$request->merge([
    'address' => $myRequest
]);

Doc: https://laravel.com/api/5.6/Illuminate/Http/Request.html#method_merge

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.