Skip to content

Commit

Permalink
Update sources
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi committed Jul 7, 2020
1 parent c27bc76 commit 3dfb050
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,24 @@ public function has(self $factory, $relationship = null)
{
return $this->newInstance([
'has' => $this->has->concat([new Relationship(
$factory, $relationship ?: Str::camel(Str::plural(class_basename($factory->modelName())))
$factory, $relationship ?: $this->guessRelationship($factory->modelName())
)]),
]);
}

/**
* Attempt to guess the relationship name for a "has" relationship.
*
* @param string $related
* @return string
*/
protected function guessRelationship(string $related)
{
$guess = Str::camel(Str::plural(class_basename($related)));

return method_exists($this->modelName(), $guess) ? $guess : Str::singular($guess);
}

/**
* Define an attached relationship for the model.
*
Expand Down Expand Up @@ -644,7 +657,7 @@ public function __call($method, $parameters)
return $this->has(
$factory
->count(is_numeric($parameters[0] ?? null) ? $parameters[0] : 1)
->state(is_array($parameters[0] ?? null) ? $parameters[0] : ($parameters[1] ?? [])),
->state((is_callable($parameters[0] ?? null) || is_array($parameters[0] ?? null)) ? $parameters[0] : ($parameters[1] ?? [])),
$relationship
);
}
Expand Down

0 comments on commit 3dfb050

Please sign in to comment.