dsshsta97935 2017-09-05 04:11
浏览 142

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

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?

  • 写回答

1条回答 默认 最新

  • dousu8767 2017-09-05 04:38
    关注

    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();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据