间接修改重载元素对数组没有影响

时间:2017-09-05 04:11:32

标签: php

使用Laravel 5.4。为什么我不能在数组中更新此字段?

在我的控制器中,我有一个受保护的字段java -jar <jarfile>

然后我获取其数据

$game

某些属性是$this->game = GameProgress::where('id', '=', $id)->lockForUpdate()->first(); 列,例如JSON字段,集合的一部分是:

regions

然后我访问/更新这样的数据:

 #attributes: array:22 [
    "id" => 13
    "name" => "DEBUGfdsf"
    "creator_id" => 1
    "game_id" => 1
    "difficulty_id" => 2
    "players_total" => 2
    "round_number" => 1
    "stage_name" => "setup"
    "active_player_id" => 2
    "active_player_turn_id" => 1
    "winner_id" => 0
    "game_points" => 0
    "locked" => 0
    "players" => "[{"id": 2, "name": "The Damage Hot", "cards": [{"id": 3, "type": "Armored", "image": "chad.png", "strength": 4, "region_id": 10, "type_image": "armored.png", "region_owned_adder": 2}, {"id": 2, "type": "Infantry", "image": "china.png", "strength": 2, "region_id": 13, "type_image": "infantry.png", "region_owned_adder": 1}, {"id": 2, "type": "Infantry", "image": "kazakhstan.png", "strength":........

但我明白了:

$this->game['regions'][$region_id - 1]['strength'] = $region_adder + $this->game['regions'][$region_id - 1]['strength'];

我似乎无法解决这个问题。如何操作数组中字段的内容?

1 个答案:

答案 0 :(得分:1)

疯狂设置。我正在以数组的形式访问集合。将->toArray()添加到记录集合中会修复它。

$this->game = GameProgress::where('id', '=', session()->get('game.id'))->lockForUpdate()->first()->toArray();