public function associateRelatedRecordsToUpdatedRecord($modelInstance, $associatedRecords, $relatedModelClass)
    {
        // Check if there are any records to sync with the pivot table
        if (count($associatedRecords) > 0)
        {
            // There's probably a function for this, but for now:
            //  (i)  create an array of the related IDs
            //  (ii) detach the existing tag IDs and attach the new tag IDs, by using SYNC

            //  (i)  create an array of the related IDs
            $newIds = array();
            foreach ($associatedRecords as $associatedId)
            {
                $newIds[] = $associatedId;
            }

            // For the purpose of saving to the pivot table, we need the method name of the related model as
            // it is in the model. As the method would be capitalized, let's un-capitalize it
            $relatedMethod = strtolower($relatedModelClass);

            // (ii) detach the existing tag IDs and attach the new tag IDs, by using SYNC
            $modelInstance->$relatedMethod()->sync($newIds);
        }
    }
public function associateRelatedRecordsToUpdatedRecord($modelInstance, $associatedRecords, $relatedModelClass)
    {
        // Check if there are any records to sync with the pivot table
        if (count($associatedRecords) > 0)
        {
            // There's probably a function for this, but for now:
            //  (i)  create an array of the related IDs
            //  (ii) detach the existing tag IDs and attach the new tag IDs, by using SYNC

            //  (i)  create an array of the related IDs
            $newIds = array();
            foreach ($associatedRecords as $associatedId)
            {
                $newIds[] = $associatedId;
            }

            // For the purpose of saving to the pivot table, we need the method name of the related model as
            // it is in the model. As the method would be capitalized, let's un-capitalize it
            $relatedMethod = strtolower($relatedModelClass);

            // (ii) detach the existing tag IDs and attach the new tag IDs, by using SYNC
            $modelInstance->$relatedMethod()->sync($newIds);
        }
    }
public function associateRelatedRecordsToUpdatedRecord ( $ modelInstance , $ associatedRecords , $ relatedModelClass ) { // Check if there are any records to sync with the pivot table if ( count ( $ associatedRecords ) > 0 ) { // There's probably a function for this, but for now: //  (i)  create an array of the related IDs //  (ii) detach the existing tag IDs and attach the new tag IDs, by using SYNC //  (i)  create an array of the related IDs $ newIds = array ( ) ; foreach ( $ associatedRecords as $ associatedId ) { $ newIds [ ] = $ associatedId ; } // For the purpose of saving to the pivot table, we need the method name of the related model as // it is in the model. As the method would be capitalized, let's un-capitalize it $ relatedMethod = strtolower ( $ relatedModelClass ) ; // (ii) detach the existing tag IDs and attach the new tag IDs, by using SYNC $ modelInstance -> $ relatedMethod ( ) -> sync ( $ newIds ) ; } }
Associate each related record with the record just updated.

@param  object    $modelInstance       object just updated
@param  array     $associatedRecords   array of id's associated with the record just updated
@param  string    $relatedNamespace    Namespace of the associated model
@param  string    $relatedModelClass   Class of the associated model
@return void
Associate each related record with the record just updated .