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

[6.x] Add wherePivotNotIn and orWherePivotNotIn methods #30671

Merged
merged 3 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,31 @@ public function orWherePivotIn($column, $values)
return $this->wherePivotIn($column, $values, 'or');
}

/**
* Set a "where not in" clause for a pivot table column.
*
* @param string $column
* @param mixed $values
* @param string $boolean
* @return $this
*/
public function wherePivotNotIn($column, $values, $boolean = 'and')
{
return $this->wherePivotIn($column, $values, $boolean, true);
}

/**
* Set an "or where not in" clause for a pivot table column.
*
* @param string $column
* @param mixed $values
* @return $this
*/
public function orWherePivotNotIn($column, $values)
{
return $this->wherePivotNotIn($column, $values, 'or');
}

/**
* Find a related model by its primary key or return new instance of the related model.
*
Expand Down
59 changes: 59 additions & 0 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,65 @@ public function testWherePivotInMethod()
$this->assertEquals($relationTag->getAttributes(), $tag->getAttributes());
}

public function testOrWherePivotInMethod()
{
$tag1 = Tag::create(['name' => Str::random()]);
$tag2 = Tag::create(['name' => Str::random()]);
$tag3 = Tag::create(['name' => Str::random()]);
$post = Post::create(['title' => Str::random()]);

DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag1->id, 'flag' => 'foo'],
]);
DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag2->id, 'flag' => 'bar'],
]);
DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag3->id, 'flag' => 'baz'],
]);

$relationTags = $post->tags()->wherePivotIn('flag', ['foo'])->orWherePivotIn('flag', ['baz'])->get();
$this->assertEquals($relationTags->pluck('id')->toArray(), [$tag1->id, $tag3->id]);
}

public function testWherePivotNotInMethod()
{
$tag1 = Tag::create(['name' => Str::random()]);
$tag2 = Tag::create(['name' => Str::random()]);
$post = Post::create(['title' => Str::random()]);

DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag1->id, 'flag' => 'foo'],
]);
DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag2->id, 'flag' => 'bar'],
]);

$relationTag = $post->tags()->wherePivotNotIn('flag', ['foo'])->first();
$this->assertEquals($relationTag->getAttributes(), $tag2->getAttributes());
}

public function testOrWherePivotNotInMethod()
{
$tag1 = Tag::create(['name' => Str::random()]);
$tag2 = Tag::create(['name' => Str::random()]);
$tag3 = Tag::create(['name' => Str::random()]);
$post = Post::create(['title' => Str::random()]);

DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag1->id, 'flag' => 'foo'],
]);
DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag2->id, 'flag' => 'bar'],
]);
DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag3->id, 'flag' => 'baz'],
]);

$relationTags = $post->tags()->wherePivotIn('flag', ['foo'])->orWherePivotNotIn('flag', ['baz'])->get();
$this->assertEquals($relationTags->pluck('id')->toArray(), [$tag1->id, $tag2->id]);
}

public function testCanUpdateExistingPivot()
{
$tag = Tag::create(['name' => Str::random()]);
Expand Down