laravel map方法踩坑记录
用map方法编辑集合的时候,集合中存在关联查询结果是相同的时候,进行修改回出现修改A会影响B的情况
例如
代码逻辑
//以下数据遍历第一次的时候,修改$item新增属性node_type,会影响第二次编辑的$item中也会node_type属性
$collect->map(function($item){
$item->node_type=$item->type;
});
这里是数据
dump($collect->toArray());
//输出结构如下
array:2 [
0 => array:9 [
"id" => 5
"role_id" => 4
"node_id" => 247
"cable_node_id" => 9
"optical_cable_id" => 116
"created_at" => "2020-01-14 16:52:36"
"updated_at" => "2020-01-14 16:52:36"
"cable_node" => array:9 [
"id" => 9
"name" => "节点3发达"
"status" => 1
"parent_id" => 0
"type" => 1
"role_id" => 4
"created_at" => "2019-11-07 19:00:33"
"updated_at" => "2019-11-30 15:03:00"
]
]
1 => array:9 [
"id" => 7
"role_id" => 4
"node_id" => 146
"cable_node_id" => 9
"optical_cable_id" => 116
"created_at" => "2020-01-15 10:32:49"
"updated_at" => "2020-01-15 10:32:49"
"cable_node" => array:9 [
"id" => 9
"name" => "节点3发达"
"status" => 1
"parent_id" => 0
"type" => 1
"role_id" => 4
"created_at" => "2019-11-07 19:00:33"
"updated_at" => "2019-11-30 15:03:00"
]
]
]
还没有评论,来说两句吧...