public function associateRelatedRecordsToNewRecord($modelInstance, $associatedRecords, $relatedNamespace, $relatedModelClass)
    {
        // Check if there are any records to insert into the pivot table
        if (count($associatedRecords) > 0)
        {
            // What is the namespace.class of the related table?
            $namespaceModelclass = $relatedNamespace . "\\". $relatedModelClass;

            // We need the repository of the related table (model?!). That repository is... well, it is
            // this repository class: the base repository class! We need a new instance with the related model.
            // So, create the new base repository instance...
            $relatedRepository = new \Lasallecms\Lasallecmsapi\Repositories\BaseRepository();

            /// ... and inject the related model class into it
            $relatedRepository->injectModelIntoRepository($namespaceModelclass);


            // 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);

            // for each record that needs to be INSERTed into the pivot table
            foreach ($associatedRecords as $associatedRecordId)
            {
                // get the record in the related table, so we can use the info to save to the pivot table
                $associatedRecord = $relatedRepository->getFind($associatedRecordId);

                // save to the pivot table
                $modelInstance->$relatedMethod()->save($associatedRecord);
            }
        }
    }
public function associateRelatedRecordsToNewRecord($modelInstance, $associatedRecords, $relatedNamespace, $relatedModelClass)
    {
        // Check if there are any records to insert into the pivot table
        if (count($associatedRecords) > 0)
        {
            // What is the namespace.class of the related table?
            $namespaceModelclass = $relatedNamespace . "\\". $relatedModelClass;

            // We need the repository of the related table (model?!). That repository is... well, it is
            // this repository class: the base repository class! We need a new instance with the related model.
            // So, create the new base repository instance...
            $relatedRepository = new \Lasallecms\Lasallecmsapi\Repositories\BaseRepository();

            /// ... and inject the related model class into it
            $relatedRepository->injectModelIntoRepository($namespaceModelclass);


            // 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);

            // for each record that needs to be INSERTed into the pivot table
            foreach ($associatedRecords as $associatedRecordId)
            {
                // get the record in the related table, so we can use the info to save to the pivot table
                $associatedRecord = $relatedRepository->getFind($associatedRecordId);

                // save to the pivot table
                $modelInstance->$relatedMethod()->save($associatedRecord);
            }
        }
    }
public function associateRelatedRecordsToNewRecord ( $ modelInstance , $ associatedRecords , $ relatedNamespace , $ relatedModelClass ) { // Check if there are any records to insert into the pivot table if ( count ( $ associatedRecords ) > 0 ) { // What is the namespace.class of the related table? $ namespaceModelclass = $ relatedNamespace . "\\" . $ relatedModelClass ; // We need the repository of the related table (model?!). That repository is... well, it is // this repository class: the base repository class! We need a new instance with the related model. // So, create the new base repository instance... $ relatedRepository = new \ Lasallecms \ Lasallecmsapi \ Repositories \ BaseRepository ( ) ; /// ... and inject the related model class into it $ relatedRepository -> injectModelIntoRepository ( $ namespaceModelclass ) ; // 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 ) ; // for each record that needs to be INSERTed into the pivot table foreach ( $ associatedRecords as $ associatedRecordId ) { // get the record in the related table, so we can use the info to save to the pivot table $ associatedRecord = $ relatedRepository -> getFind ( $ associatedRecordId ) ; // save to the pivot table $ modelInstance -> $ relatedMethod ( ) -> save ( $ associatedRecord ) ; } } }
Associate each related record with the record just created

@param  object    $modelInstance       object just created
@param  array     $associatedRecords   array of id's associated with the record just created
@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 created