src/Eloquent/HasMany.php
public function add($record, array $relationship, EncodingParametersInterface $parameters)
    {
        $related = $this->findRelated($record, $relationship);
        $relation = $this->getRelation($record, $this->key);

        $existing = $relation
            ->getQuery()
            ->whereKey($related->modelKeys())
            ->get();

        $relation->saveMany($related->diff($existing));
        $record->refresh(); // in case the relationship has been cached.

        return $record;
    }
public function add($record, array $relationship, EncodingParametersInterface $parameters)
    {
        $related = $this->findRelated($record, $relationship);
        $relation = $this->getRelation($record, $this->key);

        $existing = $relation
            ->getQuery()
            ->whereKey($related->modelKeys())
            ->get();

        $relation->saveMany($related->diff($existing));
        $record->refresh(); // in case the relationship has been cached.

        return $record;
    }
public function add ( $ record , array $ relationship , EncodingParametersInterface $ parameters ) { $ related = $ this -> findRelated ( $ record , $ relationship ) ; $ relation = $ this -> getRelation ( $ record , $ this -> key ) ; $ existing = $ relation -> getQuery ( ) -> whereKey ( $ related -> modelKeys ( ) ) -> get ( ) ; $ relation -> saveMany ( $ related -> diff ( $ existing ) ) ; $ record -> refresh ( ) ; // in case the relationship has been cached. return $ record ; }
Add records to the relationship.

Note that the spec says that duplicates MUST NOT be added. The default Laravel
behaviour is to add duplicates, therefore we need to do some work to ensure
that we only add the records that are not already in the relationship.

@param Model $record
@param array $relationship
@param EncodingParametersInterface $parameters
@return Model
Add records to the relationship .