Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conditions being dropped when updating custom pivot classes #55720

Copy link
Copy link
Closed
@cetetesoft

Description

@cetetesoft
Issue body actions

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" = ?"

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.