Skip to content

Commit

Permalink
Added unsetRelation method to Eloquent models
Browse files Browse the repository at this point in the history
  • Loading branch information
IsraelOrtuno committed Jun 6, 2018
1 parent 4b736f7 commit 8d8fc13
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Database/DatabaseEloquentRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 8d8fc13

Please sign in to comment.