private function sync($id, $attributes)
    {
        // Return if the ID is invalid, or attributes is not an array
        if (!isset($id) || !isset($attributes) || empty($attributes)) {
            return $this;
        }

        // Return if there are no relationships of that type
        if (!isset($this->relationships['belongsToMany']) || empty($this->relationships['belongsToMany'])) {
            return $this;
        }

        // Sync each relationship
        foreach ($this->relationships['belongsToMany'] as $join) {

            // The sync option has to be true, the related attribute an array
            if (isset($join['sync']) && $join['sync'] && isset($attributes[$join['alias']]) && is_array($attributes[$join['alias']])) {

                // Delete old values
                $delete = $this->db()->table($join['pivot'])->where($join['localKey'], $id, '=')->delete();

                // Insert new values
                foreach ($attributes[$join['alias']] as $value) {
                    $this->db()->table($join['pivot'])->insert([$join['localKey'] => $id, $join['foreignKey'] => $value]);
                }
            }
        }

        // Return model instance
        return $this;
    }
private function sync($id, $attributes)
    {
        // Return if the ID is invalid, or attributes is not an array
        if (!isset($id) || !isset($attributes) || empty($attributes)) {
            return $this;
        }

        // Return if there are no relationships of that type
        if (!isset($this->relationships['belongsToMany']) || empty($this->relationships['belongsToMany'])) {
            return $this;
        }

        // Sync each relationship
        foreach ($this->relationships['belongsToMany'] as $join) {

            // The sync option has to be true, the related attribute an array
            if (isset($join['sync']) && $join['sync'] && isset($attributes[$join['alias']]) && is_array($attributes[$join['alias']])) {

                // Delete old values
                $delete = $this->db()->table($join['pivot'])->where($join['localKey'], $id, '=')->delete();

                // Insert new values
                foreach ($attributes[$join['alias']] as $value) {
                    $this->db()->table($join['pivot'])->insert([$join['localKey'] => $id, $join['foreignKey'] => $value]);
                }
            }
        }

        // Return model instance
        return $this;
    }
private function sync ( $ id , $ attributes ) { // Return if the ID is invalid, or attributes is not an array if ( ! isset ( $ id ) || ! isset ( $ attributes ) || empty ( $ attributes ) ) { return $ this ; } // Return if there are no relationships of that type if ( ! isset ( $ this -> relationships [ 'belongsToMany' ] ) || empty ( $ this -> relationships [ 'belongsToMany' ] ) ) { return $ this ; } // Sync each relationship foreach ( $ this -> relationships [ 'belongsToMany' ] as $ join ) { // The sync option has to be true, the related attribute an array if ( isset ( $ join [ 'sync' ] ) && $ join [ 'sync' ] && isset ( $ attributes [ $ join [ 'alias' ] ] ) && is_array ( $ attributes [ $ join [ 'alias' ] ] ) ) { // Delete old values $ delete = $ this -> db ( ) -> table ( $ join [ 'pivot' ] ) -> where ( $ join [ 'localKey' ] , $ id , '=' ) -> delete ( ) ; // Insert new values foreach ( $ attributes [ $ join [ 'alias' ] ] as $ value ) { $ this -> db ( ) -> table ( $ join [ 'pivot' ] ) -> insert ( [ $ join [ 'localKey' ] => $ id , $ join [ 'foreignKey' ] => $ value ] ) ; } } } // Return model instance return $ this ; }
Syncs many to many relationships

@param $attributes Model attributes
@return object Model instance
Syncs many to many relationships