public function detach($related)
{
if ($related instanceof Model === false) {
$related = $this->related->where('_id', new MongoID($related))->first();
}
$ids = array();
$parent_id = $this->parent->getKey();
foreach ((array) $related->getAttribute($this->foreignKey) as $value) {
if ((string) $value == $parent_id) {
continue;
}
$ids[(string) $value] = new MongoID((string) $value);
}
$related->setAttribute($this->foreignKey, array_values($ids));
$related->save();
$ids = array();
foreach ((array) $this->parent->getAttribute($this->otherKey) as $value) {
if ((string) $value == $related->getKey()) {
continue;
}
$ids[(string) $value] = new MongoID((string) $value);
}
$this->parent->setAttribute($this->otherKey, array_values($ids));
$this->parent->save();
}