Nayeli Flores Nayeli Flores - 7 months ago 56
PHP Question

Eloquent updates all rows

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 |
+---------+-----------+-----------+-----------+

Answer

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.