From 3dfb050da48e436744928a0821544d9670848c92 Mon Sep 17 00:00:00 2001 From: Bastien Philippe Date: Tue, 7 Jul 2020 16:40:05 +0200 Subject: [PATCH] Update sources --- src/Factory.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Factory.php b/src/Factory.php index 13efa40..9041054 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -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. * @@ -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 ); }