factory = $factory; $this->pivot = $pivot; $this->relationship = $relationship; } /** * Create the attached relationship for the given model. * * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function createFor(Model $model) { $factoryInstance = $this->factory instanceof Factory; if ($factoryInstance) { $relationship = $model->{$this->relationship}(); } Collection::wrap($factoryInstance ? $this->factory->prependState($relationship->getQuery()->pendingAttributes)->create([], $model) : $this->factory)->each(function ($attachable) use ($model) { $model->{$this->relationship}()->attach( $attachable, is_callable($this->pivot) ? call_user_func($this->pivot, $model) : $this->pivot ); }); } /** * Specify the model instances to always use when creating relationships. * * @param \Illuminate\Support\Collection $recycle * @return $this */ public function recycle($recycle) { if ($this->factory instanceof Factory) { $this->factory = $this->factory->recycle($recycle); } return $this; } }