From 8d8fc13597cec1caa12b24c3565390b8678265e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Israel=20Ortu=C3=B1o?= Date: Wed, 6 Jun 2018 18:26:05 +0200 Subject: [PATCH 1/3] Added unsetRelation method to Eloquent models --- .../Eloquent/Concerns/HasRelationships.php | 15 +++++++++++++++ tests/Database/DatabaseEloquentRelationTest.php | 9 +++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index 50202aa2152f..84cd09010ecc 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -706,6 +706,21 @@ public function setRelation($relation, $value) return $this; } + /** + * Unset an existing relation. + * + * @param string $relation + * @return $this + */ + public function unsetRelation($relation) + { + if ($this->relationLoaded($relation)) { + unset($this->relations[$relation]); + } + + return $this; + } + /** * Set the entire relations array on the model. * diff --git a/tests/Database/DatabaseEloquentRelationTest.php b/tests/Database/DatabaseEloquentRelationTest.php index 52e83d46ca31..7bf340a87518 100755 --- a/tests/Database/DatabaseEloquentRelationTest.php +++ b/tests/Database/DatabaseEloquentRelationTest.php @@ -25,6 +25,15 @@ public function testSetRelationFail() $this->assertArrayNotHasKey('foo', $parent->toArray()); } + public function testUnsetExistingRelation() + { + $parent = new EloquentRelationResetModelStub; + $relation = new EloquentRelationResetModelStub; + $parent->setRelation('foo', $relation); + $parent->unsetRelation('foo'); + $this->assertFalse($parent->relationLoaded('foo')); + } + public function testTouchMethodUpdatesRelatedTimestamps() { $builder = m::mock(Builder::class); From de003ac9be6cf2a596e984599c0a677bd2d979ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Israel=20Ortu=C3=B1o?= Date: Wed, 6 Jun 2018 18:39:53 +0200 Subject: [PATCH 2/3] Remove space --- tests/Database/DatabaseEloquentRelationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/DatabaseEloquentRelationTest.php b/tests/Database/DatabaseEloquentRelationTest.php index 7bf340a87518..8ec2537a97f4 100755 --- a/tests/Database/DatabaseEloquentRelationTest.php +++ b/tests/Database/DatabaseEloquentRelationTest.php @@ -25,7 +25,7 @@ public function testSetRelationFail() $this->assertArrayNotHasKey('foo', $parent->toArray()); } - public function testUnsetExistingRelation() + public function testUnsetExistingRelation() { $parent = new EloquentRelationResetModelStub; $relation = new EloquentRelationResetModelStub; From 7538abb8649e18db0a0a58d47d07d62042ddd2c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Israel=20Ortu=C3=B1o?= Date: Wed, 6 Jun 2018 18:56:25 +0200 Subject: [PATCH 3/3] Removed relationship existence check --- .../Database/Eloquent/Concerns/HasRelationships.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php index 84cd09010ecc..49f4544d4177 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php @@ -714,9 +714,7 @@ public function setRelation($relation, $value) */ public function unsetRelation($relation) { - if ($this->relationLoaded($relation)) { - unset($this->relations[$relation]); - } + unset($this->relations[$relation]); return $this; }