LMongo\Eloquent\Relations\BelongsToMany::attach PHP Метод

attach() публичный Метод

Attach a model to the parent.
public attach ( mixed $related ) : void
$related mixed
Результат void
    public function attach($related)
    {
        if ($related instanceof Model === false) {
            $related = $this->related->where('_id', new MongoID($related))->first();
        }
        $ids = array();
        foreach ((array) $related->getAttribute($this->foreignKey) as $value) {
            $ids[(string) $value] = new MongoID((string) $value);
        }
        $parent_id = $this->parent->getKey();
        $ids[$parent_id] = new MongoID($parent_id);
        $related->setAttribute($this->foreignKey, array_values($ids));
        $related->save();
        $ids = array();
        foreach ((array) $this->parent->getAttribute($this->otherKey) as $value) {
            $ids[(string) $value] = new MongoID((string) $value);
        }
        $ids[$related->getKey()] = new MongoID($related->getKey());
        $this->parent->setAttribute($this->otherKey, array_values($ids));
        $this->parent->save();
    }