LMongo\Eloquent\Relations\BelongsToMany::sync PHP Method

sync() public method

Sync the intermediate tables with a list of IDs.
public sync ( array $ids ) : void
$ids array
return void
    public function sync(array $ids)
    {
        $ids = array_map(function ($value) {
            return (string) $value;
        }, $ids);
        // First we need to attach any of the associated models that are not currently
        // in this joining table. We'll spin through the given IDs, checking to see
        // if they exist in the array of current ones, and if not we will insert.
        $current = (array) $this->parent->getAttribute($this->otherKey);
        $current = array_map(function ($value) {
            return (string) $value;
        }, $current);
        foreach ($ids as $id) {
            if (!in_array($id, $current)) {
                $this->attach($id);
            }
        }
        // Next, we will take the differences of the currents and given IDs and detach
        // all of the entities that exist in the "current" array but are not in the
        // the array of the IDs given to the method which will complete the sync.
        $detach_ids = array_diff($current, $ids);
        foreach ($detach_ids as $detach) {
            $this->detach($detach);
        }
    }