2017-09-05 32 views
0

使用Laravel 5.4。爲什麼我不能更新數組中的這個字段?重載元素的間接修改對陣列沒有影響

在我的控制器中,我有一個受保護的字段$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']; 

但我得到這個:

Indirect modification of overloaded element of App\Models\GameProgress has no effect 

我似乎無法解決這個問題。如何操作數組中的字段內容?

+0

似乎'$ this-> game'是一個'GameProgress'實例。它的API是什麼?它提供了一種操縱數據的方式嗎? – Phil

+0

$ this->遊戲是控制器全局可用的受保護變量。它包含GameProgress集合的一個實例。 – TheRealPapa

回答

0

瘋狂設置英寸我正在訪問集合作爲數組。將->toArray()添加到記錄收集固定它。

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