Example #1
0
 /**
  * Attaches the given model(s) to the current one via its relationship
  *
  * This is kind of like calling save on an individual relationship,
  * except that we're attaching the models back to the parent entity.
  * This is helpful if you're going to call saveAndPropagate and want
  * to pass the parent object back to a view in the event of a save error.
  *
  * @param   string        $relationship  The relationship to invoke
  * @param   array|object  $models        The model or models to attach
  * @return  $this
  * @since   2.0.0
  **/
 public function attach($relationship, $models)
 {
     // If we have an array, we'll put it into a rows object
     // (like we would if we were fetching the results from the db)
     if (is_array($models)) {
         $rows = new Rows();
         foreach ($models as $model) {
             $rows->push($model);
         }
     } else {
         // Otherwise it's just a single model
         $rows = $models;
     }
     // Get our rows associated according to their relationship type
     // This means we add related keys, etc to the passed in rows
     $rows = $this->{$relationship}()->associate($rows);
     $this->addRelationship($relationship, $rows);
     return $this;
 }