Skip to content

detach / sync generate wrong sql when wherePivotIn exists #14397

Closed
@jhdxr

Description

$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.

Activity

themsaid

themsaid commented on Jul 21, 2016

@themsaid
Member

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

jhdxr

jhdxr commented on Jul 21, 2016

@jhdxr
ContributorAuthor

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

GrahamCampbell commented on Jul 21, 2016

@GrahamCampbell
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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      detach / sync generate wrong sql when wherePivotIn exists · Issue #14397 · laravel/framework