Sync model tenants.

@param mixed $tenants
@param bool  $detaching

@return $this
176917
public function syncTenants($tenants, bool $detaching = true)
    {
        // Find tenants
        $tenants = $this->prepareTenantIds($tenants);

        // Sync model tenants
        $this->tenants()->sync($tenants, $detaching);

        return $this;
    }
608493
public function detach($ids = null, $touch = true)
    {
        if (is_null($ids)) {
            $ids = $this->query->pluck($this->query->qualifyColumn($this->relatedKey))->toArray();
        }

        list($idsOnly) = $this->getIdsWithAttributes($ids);

        $this->parent->fireModelEvent('pivotDetaching', true, $this->getRelationName(), $idsOnly);
        $parentResult = parent::detach($ids, $touch);
        $this->parent->fireModelEvent('pivotDetached', false, $this->getRelationName(), $idsOnly);

        return $parentResult;
    }
67892
public function sync($ids, $detaching = true)
    {
        $changes = [
            'attached' => [],
            'detached' => [],
            'updated'  => [],
        ];

        $pk = $this->parent->getPk();

        $current = $this->pivot
            ->where($this->localKey, $this->parent->$pk)
            ->column($this->foreignKey);

        $records = [];

        foreach ($ids as $key => $value) {
            if (!is_array($value)) {
                $records[$value] = [];
            } else {
                $records[$key] = $value;
            }
        }

        $detach = array_diff($current, array_keys($records));

        if ($detaching && count($detach) > 0) {
            $this->detach($detach);
            $changes['detached'] = $detach;
        }

        foreach ($records as $id => $attributes) {
            if (!in_array($id, $current)) {
                $this->attach($id, $attributes);
                $changes['attached'][] = $id;
            } elseif (count($attributes) > 0 && $this->attach($id, $attributes)) {
                $changes['updated'][] = $id;
            }
        }

        return $changes;
    }
1043264
public function hasAllTenants($tenants): bool
    {
        $tenants = $this->prepareTenantIds($tenants);

        return collect($tenants)->diff($this->tenants->pluck('id'))->isEmpty();
    }
1123968
protected function syncTagIds($ids, string $type = null, $detaching = true)
    {
        $isUpdated = false;

        // Get a list of tag_ids for all current tags
        $current = $this->tags()
            ->newPivotStatement()
            ->where('taggable_id', $this->getKey())
            ->where('taggable_type', $this->getMorphClass())
            ->when($type !== null, function ($query) use ($type) {
                $tagModel = $this->tags()->getRelated();

                return $query->join(
                    $tagModel->getTable(),
                    'taggables.tag_id',
                    '=',
                    $tagModel->getTable().'.'.$tagModel->getKeyName()
                )
                    ->where('tags.type', $type);
            })
            ->pluck('tag_id')
            ->all();

        // Compare to the list of ids given to find the tags to remove
        $detach = array_diff($current, $ids);
        if ($detaching && count($detach) > 0) {
            $this->tags()->detach($detach);
            $isUpdated = true;
        }

        // Attach any new ids
        $attach = array_diff($ids, $current);
        if (count($attach) > 0) {
            collect($attach)->each(function ($id) {
                $this->tags()->attach($id, []);
            });
            $isUpdated = true;
        }

        // Once we have finished attaching or detaching the records, we will see if we
        // have done any attaching or detaching, and if we have we will touch these
        // relationships if they are configured to touch on any database updates.
        if ($isUpdated) {
            $this->tags()->touchIfTouching();
        }
    }
956360
public function detachRole($role, $reload = true)
    {
        event(new DetachingRoleFromPermission($this, $role));
        $results = $this->roles()->detach($role);
        event(new DetachedRoleFromPermission($this, $role, $results));

        $this->loadRoles($reload);

        return $results;
    }
626157
public function detachPermission($permission)
    {
        $permission = Helper::getIdFor($permission, 'permission');

        $this->permissions()->detach($permission);
        $this->flushCache();
        $this->fireLaratrustEvent("permission.detached", [$this, $permission]);

        return $this;
    }
50072
public function detachPermission(...$permissions)
    {
        $this->eachPermission($permissions, function($permission) {
            if ($this->hasDirectPermission($permission)) {
                $this->permissions()->detach($permission->id);
            }
        });

        return $this;
    }
601369
public function detach()
    {
        return $this->execute($this->getActionAlias('detach'), [
            $this->getSlug(),
            $this->getCallback(),
            $this->getArgument('piority'),
        ]);
    }
934541
public function attachRoles($roles)
    {
        if ($roles instanceof Role) {
            $roles = [$roles->getKey()];
        }

        $this->roles()->sync($roles, false);

        unset($this->relations['roles']);

        return $this;
    }
499555
public function detach()
    {
        $this->calendar->getEvents()->removeEvent($this);
        $this->calendar = null;

        return $this;
    }
992482
public function setAttachStderr(bool $attachStderr = false)
    {
        $this->attachStderr = $attachStderr;

        $this->raw = array_merge($this->raw, ['AttachStderr' => $attachStderr]);

        return $this;
    }
133100
protected function detachExcept($entities)
    {
        $query = $this->query->getQuery()->from($this->relatedMap->getTable());

        if (count($entities) > 0) {
            $keys = $this->getKeys($entities);
            $query->whereNotIn($this->relatedMap->getKeyName(), $keys);
        }

        $parentKey = $this->parentMap->getKeyName();

        $query->where($this->getPlainForeignKey(), '=', $this->parent->getEntityAttribute($parentKey))
            ->update([$this->getPlainForeignKey() => null]);
    }
693831
public function detach() {
        $stream = $this->stream;
        $this->stream = null;
        $this->flen = null;
        $this->offsetList = null;
        $this->cursor = null;
        return $stream;
    }
893399
public function detachAllRoles($reload = true)
    {
        event(new DetachingRolesFromUser($this));
        $results = $this->roles()->detach();
        event(new DetachedRolesFromUser($this, $results));

        $this->loadRoles($reload);

        return $results;
    }
863350
public function syncCounters($ids) {
        foreach ($this->getCounters() as $alias => $counter) {
            $relation = $this->getModel()->getRelation($alias);

            switch ($relation->getType()) {
                case Relation::MANY_TO_MANY:
                    $this->_syncManyToMany($relation, $ids, $counter);
                break;
                case Relation::MANY_TO_ONE:
                    $this->_syncManyToOne($relation, $ids, $counter);
                break;
            }
        }

        // Reset cache for this sync
        $this->flushCache();

        return $this;
    }
1146603
public function setAttachStdout(bool $attachStdout = false)
    {
        $this->attachStdout = $attachStdout;

        $this->raw = array_merge($this->raw, ['AttachStdout' => $attachStdout]);

        return $this;
    }