【问题标题】:Can't update collection in Laravel eloquent无法在 Laravel eloquent 中更新集合
【发布时间】:2021-05-01 16:59:30
【问题描述】:

有一个表结构如下:(主键:user_id,record_id)

+---------+-----------+-------+
| user_id | record_id | value |
+---------+-----------+-------+
|       1 |         1 |   100 |
|       1 |         2 |   200 |
|       2 |         1 |   300 |
|       2 |         2 |   400 |
+---------+-----------+-------+

当我像这样在控制器中通过 eloquent-query 更改 value 参数时:

$playerRecord = Test::where('user_id', '=', $player_id)->where('record_id', '=', '1')->first();
$playerRecord->value = $user_value1;            
$playerRecord->save();

我有一个错误:https://flareapp.io/share/47qg8ZEm#F49

如果我在模型中只定义一个主键,那么所有具有此键的记录都会更新,尽管我强制更新了特定记录。

【问题讨论】:

  • 你可以在查询中直接使用 update()
  • 这能回答你的问题吗? stackoverflow.com/questions/36332005/…
  • 似乎这一行抛出一个空结果Test::where('user_id', '=', $player_id)->where('record_id', '=', '1')->first();

标签: php laravel


【解决方案1】:
  $playerRecord = Test::where('user_id', '=', $player_id)->where('record_id', '=', '1')->update(['value'=>$user_value1]);
    if($playerRecord){
     return "data updated";
}else{
    return "data not found";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2018-02-25
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    相关资源
    最近更新 更多