From 2e8575df57441ec4f9d06895d3778ad4460fe7c0 Mon Sep 17 00:00:00 2001 From: Isma Date: Sun, 24 Nov 2019 17:31:17 +0100 Subject: [PATCH 1/3] Add wherePivotNotIn and orWherePivotNotIn --- .../Eloquent/Relations/BelongsToMany.php | 25 ++++++++++++ .../Database/EloquentBelongsToManyTest.php | 39 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 0bdf4fad0f21..48d626ba9bf3 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -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. * diff --git a/tests/Integration/Database/EloquentBelongsToManyTest.php b/tests/Integration/Database/EloquentBelongsToManyTest.php index 3941ee613c47..e1dfc3cc0fa7 100644 --- a/tests/Integration/Database/EloquentBelongsToManyTest.php +++ b/tests/Integration/Database/EloquentBelongsToManyTest.php @@ -658,6 +658,45 @@ public function testWherePivotInMethod() $this->assertEquals($relationTag->getAttributes(), $tag->getAttributes()); } + 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()]); From 122285ac7bf4719ff53a7bf620843ddd58f4ef19 Mon Sep 17 00:00:00 2001 From: Isma Date: Sun, 24 Nov 2019 17:34:30 +0100 Subject: [PATCH 2/3] Add a test for orWherePivotIn --- .../Database/EloquentBelongsToManyTest.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/Integration/Database/EloquentBelongsToManyTest.php b/tests/Integration/Database/EloquentBelongsToManyTest.php index e1dfc3cc0fa7..b1e9904caa51 100644 --- a/tests/Integration/Database/EloquentBelongsToManyTest.php +++ b/tests/Integration/Database/EloquentBelongsToManyTest.php @@ -658,6 +658,27 @@ 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()]); From aabd8c274431cce0d543535513e95ee872d757cc Mon Sep 17 00:00:00 2001 From: Ismail E Date: Sun, 24 Nov 2019 18:03:44 +0100 Subject: [PATCH 3/3] Fix style --- tests/Integration/Database/EloquentBelongsToManyTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Integration/Database/EloquentBelongsToManyTest.php b/tests/Integration/Database/EloquentBelongsToManyTest.php index b1e9904caa51..d3a2ed4f4e5e 100644 --- a/tests/Integration/Database/EloquentBelongsToManyTest.php +++ b/tests/Integration/Database/EloquentBelongsToManyTest.php @@ -717,7 +717,6 @@ public function testOrWherePivotNotInMethod() $this->assertEquals($relationTags->pluck('id')->toArray(), [$tag1->id, $tag2->id]); } - public function testCanUpdateExistingPivot() { $tag = Tag::create(['name' => Str::random()]);