diff --git a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php
index 47d0079aaf11131cb54b1890c17ffec2a9dd9728..4c9880a1fd76caec33f59ceee55ebc9f56ee1c04 100644
--- a/src/Jenssegers/Mongodb/Relations/BelongsToMany.php
+++ b/src/Jenssegers/Mongodb/Relations/BelongsToMany.php
@@ -170,6 +170,9 @@ class BelongsToMany extends EloquentBelongsToMany {
$query = $this->newParentQuery();
+ // Generate a new related query instance
+ $related = $this->related->newInstance();
+
// If associated IDs were passed to the method we will only delete those
// associations, otherwise all of the association ties will be broken.
// We'll return the numbers of affected rows when we do the deletes.
@@ -189,6 +192,9 @@ class BelongsToMany extends EloquentBelongsToMany {
{
$query->pull($this->otherKey, $id);
}
+
+ // Remove the relation from the related model
+ $related->pull($this->foreignKey, $this->parent->getKey());
return count($ids);
}
diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php
index b1ae56b60029bfaedd44c531e0587e191fd2e417..39edbb0305e070d2f7862a231dec22cc715e3243 100644
--- a/tests/RelationsTest.php
+++ b/tests/RelationsTest.php
@@ -204,8 +204,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
);
$moreClients = array(
- Client::create(array('name' => 'Boloni Ltd.'))->_id,
- Client::create(array('name' => 'Meatballs Inc.'))->_id
+ Client::create(array('name' => 'synced Boloni Ltd.'))->_id,
+ Client::create(array('name' => 'synced Meatballs Inc.'))->_id
);
// Sync multiple records
@@ -224,7 +224,10 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$user = User::with('clients')->find($user->_id);
- // Assert there are now 4 client objects in the relationship
- $this->assertCount(4, $user->clients);
+ // Assert there are now still 2 client objects in the relationship
+ $this->assertCount(2, $user->clients);
+ // Assert that the new relationships name start with synced
+ $this->assertStringStartsWith('synced', $user->clients[0]->name);
+ $this->assertStringStartsWith('synced', $user->clients[1]->name);
}
}