Closed
Description
Laravel Version
12.12.0
PHP Version
8.3.6
Database Driver & Version
No response
Description
After fixing #55026, there's a new issue when updating pivot tables with sync()
. Conditions set by wherePivot()
are no longer being applied, which results in more rows being updated than expected.
Steps To Reproduce
This is how I define my relationship:
public function texts(int $reportId):BelongsToMany{
return $this->belongsToMany(Text::class,'orders_texts','order_id','text_id')
->wherePivot('report_id',$reportId)
->using(OrderText::class)
->withPivot(
[
'report_id',
'included',
'content',
'status',
'comment'
]
)
->withTimestamps();
}
I then run the following code:
$order->texts(123)->sync(
[
1=>[
'included'=>true,
'content'=>'Test',
'status'=>null,
'comment'=>null
]
]
);
The query actually being run is the following:
update "orders_texts" set "content" = ?, "updated_at" = ? where "order_id" = ? and "text_id" = ?"
This is missing the report_id
condition which I set with wherePivot()
, so all the rows in orders_texts
with text_id=1
and order_id=5
(5 is the order ID) are being updated.
The query should include report_id
, ie:
update "orders_texts" set "content" = ?, "updated_at" = ? where "report_id" = ? and "order_id" = ? and "text_id" = ?"