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) { Collection::wrap($this->factory instanceof Factory ? $this->factory->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 ); }); } }