protected function updateBelongsToManyRelation($eloquentModel, $name, $action)
    {
        $eloquentCollection = $eloquentModel->getRelation($name);

        if (! $eloquentCollection instanceof \Illuminate\Database\Eloquent\Collection) {
            throw new Exception("Many-to-many relation '".$name."' is not a valid collection");
        }

        // get related keys
        $keys = [];

        foreach($eloquentCollection as $item) {
            $keys[] = $item->getKey();
        }

        // attach/sync/detach keys
        if ($action == 'insert') {
            $eloquentModel->{$name}()->attach($keys);
        }
        if ($action == 'update') {
            $eloquentModel->{$name}()->sync($keys);
        }
        if ($action == 'delete') {
            $eloquentModel->{$name}()->detach($keys);
        }
    }
protected function updateBelongsToManyRelation($eloquentModel, $name, $action)
    {
        $eloquentCollection = $eloquentModel->getRelation($name);

        if (! $eloquentCollection instanceof \Illuminate\Database\Eloquent\Collection) {
            throw new Exception("Many-to-many relation '".$name."' is not a valid collection");
        }

        // get related keys
        $keys = [];

        foreach($eloquentCollection as $item) {
            $keys[] = $item->getKey();
        }

        // attach/sync/detach keys
        if ($action == 'insert') {
            $eloquentModel->{$name}()->attach($keys);
        }
        if ($action == 'update') {
            $eloquentModel->{$name}()->sync($keys);
        }
        if ($action == 'delete') {
            $eloquentModel->{$name}()->detach($keys);
        }
    }
protected function updateBelongsToManyRelation ( $ eloquentModel , $ name , $ action ) { $ eloquentCollection = $ eloquentModel -> getRelation ( $ name ) ; if ( ! $ eloquentCollection instanceof \ Illuminate \ Database \ Eloquent \ Collection ) { throw new Exception ( "Many-to-many relation '" . $ name . "' is not a valid collection" ) ; } // get related keys $ keys = [ ] ; foreach ( $ eloquentCollection as $ item ) { $ keys [ ] = $ item -> getKey ( ) ; } // attach/sync/detach keys if ( $ action == 'insert' ) { $ eloquentModel -> { $ name } ( ) -> attach ( $ keys ) ; } if ( $ action == 'update' ) { $ eloquentModel -> { $ name } ( ) -> sync ( $ keys ) ; } if ( $ action == 'delete' ) { $ eloquentModel -> { $ name } ( ) -> detach ( $ keys ) ; } }
Update a belongsToMany or morphToMany relation.

@param \ProAI\Datamapper\Eloquent\Model $eloquentModel
@param string $name
@param string $action
@return void
Update a belongsToMany or morphToMany relation .