10000 Conditions being dropped when updating custom pivot classes · Issue #55720 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

Conditions being dropped when updating custom pivot classes #55720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
8000 cetetesoft opened this issue May 12, 2025 · 2 comments
Open

Conditions being dropped when updating custom pivot classes #55720

cetetesoft opened this issue May 12, 2025 · 2 comments

Comments

@cetetesoft
Copy link

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

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@ShaungBhone
Copy link
ShaungBhone commented May 14, 2025

You need ->withPivotValue('report_id', 123);. wherePivot is only for filter (reading). sync() is writing. Doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
0