Preshan Pradeepa
Preshan Pradeepa

Reputation: 683

How to add item to an array inside a collection in Laravel

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

Upvotes: 0

Views: 2072

Answers (1)

Alessandro.Vegna
Alessandro.Vegna

Reputation: 1260

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

Upvotes: 1

Related Questions