0

There is a table with the following structure: (primary keys: user_id, record_id)

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

When i change value parameter over eloquent-query in my Controller like this:

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

I have an error: https://flareapp.io/share/47qg8ZEm#F49

If I define only one primary key in the model, all records with this key are updated, despite the fact that I forced to update a specific record.

Schekhovtsov
  • 101
  • 2
  • 10
  • u can use update() directly in your query – JEJ May 01 '21 at 08:49
  • Does this answer your question? https://stackoverflow.com/questions/36332005/laravel-model-with-two-primary-keys-update – shahidiqbal May 01 '21 at 08:50
  • Seems like this line throw an empty result `Test::where('user_id', '=', $player_id)->where('record_id', '=', '1')->first();` – STA May 01 '21 at 08:52

1 Answers1

0
  $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";
}
JEJ
  • 814
  • 5
  • 21