如何将项添加到Laravel中集合内的数组中

问题描述 投票:-1回答:1

我有一个Laravel Request对象(集合),并且在这个(地址数组)中有一个数组我想在这个数组中添加一个项目。我尝试了$request->address['state'] = 'test';并发生了以下错误。

间接修改重载属性Illuminate \ Http \ Request :: $ address无效

Here is the request collection

我想在这个img中添加一个突出显示的项目

php laravel-5.1
1个回答
2
投票

从请求中获取关联数组并使用它的最简单方法。

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

否则,您必须修改添加此代码所需的请求对象:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.