2
votes

Basically, I try to do an update to one row in a table containing two primary keys. Primary keys are Trad_id and Trad_lang. For each differen row I want to update with the correct data, but Eloquent updates all rows with the same Trad_id.

Why is Eloquent updating all rows?

$tradInt = \Model\Traduction::where('Trad_id', $reference->Trad_id)->where("Trad_lang", "INTERNE")->first();
$tradInt->Trad_text = 'ABC';
$tradInt->save();

$tradExt = \Model\Traduction::where('Trad_id', $reference->Trad_id)->where("Trad_lang", "EXTERNE")->first();
$tradExt->Trad_text = 123;
$tradExt->save();

+---------+-----------+-----------+-----------+
| Trad_id | Trad_lang | Trad_type | Trad_text |
+---------+-----------+-----------+-----------+
|    1206 | INTERNE   |           | 123       |
|    1206 | EXTERNE   |           | 123       |
|    1206 | FR        |           | 123       |
+---------+-----------+-----------+-----------+
1
The first() method will only ever return one item so are you sure it's updating more than one row in the DB per update statement? Try dd() the result of your query before updating so you can see what is being updated. Also, does your table require a composite primary key? Maybe that's what is causing the issue? - haakym

1 Answers

1
votes

Problem is that Laravel doesn't support composite keys and for sure they won't support it in future. So the solution is to use update query:

\Model\Traduction::where('Trad_id', $reference->Trad_id)
    ->where("Trad_lang", "EXTERNE")
    ->update(['Trad_text' => 123]);

Here is this issue:

https://github.com/laravel/framework/issues/5355

See the Taylor's response for that.