Reputation: 4539
Using Laravel 5.4. Why can't I update this field in an array?
In my controller I have a protected field $game
.
I then fetch its data
$this->game = GameProgress::where('id', '=', $id)->lockForUpdate()->first();
Some of the properties are JSON
columns, such as the regions
field, a part of the collection is:
#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":........
I then access / update the data like this:
$this->game['regions'][$region_id - 1]['strength'] = $region_adder + $this->game['regions'][$region_id - 1]['strength'];
But I get this:
Indirect modification of overloaded element of App\Models\GameProgress has no effect
I cannot seem to work around this. How do I manipulate the content of fields in the arrays?
Upvotes: 0
Views: 7361
Reputation: 4539
Insanity setting in. I was accessing a collection as an array. Adding the ->toArray()
to the record collection fixed it.
$this->game = GameProgress::where('id', '=', session()->get('game.id'))->lockForUpdate()->first()->toArray();
Upvotes: 1