Skip to content
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

detach / sync generate wrong sql when wherePivotIn exists #14397

Closed
jhdxr opened this issue Jul 20, 2016 · 3 comments
Closed

detach / sync generate wrong sql when wherePivotIn exists #14397

jhdxr opened this issue Jul 20, 2016 · 3 comments

Comments

@jhdxr
Copy link
Contributor

jhdxr commented Jul 20, 2016

$this->pivotWheres[] = func_get_args();

wherePivotIn saves the all the params into $this->pivotWheres, which leads to newPivotQuery consider them as normal where clause instead of whereIn clause.

@themsaid
Copy link
Member

How do you use sync() with wherePivotIn()?

@jhdxr
Copy link
Contributor Author

jhdxr commented Jul 21, 2016

Here's the example

class Lesson extends Model
{
  public function students()
  {
    return $this->belongsToMany(User::class)->wherePivotIn('status', [2,3]);
  }
}
$lesson->students()->detach(Auth::user())

assume the primary key of lesson is 1, and 2 for current user, then the correct sql should be

delete from `xxxxx` where `status` in ('2', '3') and `lesson_id` = '1' and `user_id` in ('2')

however, laravel generate following sql now:

delete from `xxxxx` where `status` = '2' and `lesson_id` = '3' and `user_id` in ('1')

the reason is BelongsToMany::wherePivotIn() saves all the params $this->pivotWheres, as well as BelongsToMany::wherePivot(). when BelongsToMany::newPivotQuery() create new query builder, it will asume everything in $this->pivotWheres is created from wherePivot() and call where() on them.

I've already created a pr to fix this issue, to store and process the params separately.

@GrahamCampbell
Copy link
Member

Closing to move discussion to the PR.

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

No branches or pull requests

3 participants