/** * Generate the model. * * @throws LackOfCoffeeException * @return Model */ public function make() { // Make model instance $model = $this->getModelInstance(true); $filling = TransformPipeline::define()->inline(function ($input) { return array_merge($input, $this->overrides); })->inline(function ($input) { return Std::map(function ($value) { return Std::value($value); }, $input); })->run($this->getMapping()); Std::each(function ($value, $key) use(&$model) { $model->{$key} = $value; }, $filling); Std::each(function ($relation, $name) use(&$model) { if (!$this->isBelongsTo($name)) { return; } $model->{$name}()->associate($this->generateRelation($name)); }, $this->relations); $model->save(); Std::each(function ($relation, $name) use(&$model) { if ($this->isBelongsTo($name)) { return; } $related = $this->generateRelation($name); if (is_array($related)) { $model->{$name}()->saveMany($related); } else { $model->{$name}()->save($related); } }, $this->relations); return $model; }