Browse Source

Throw error if factory instantiation errors

shalvah 3 years ago
parent
commit
5eb0d72f9b
1 changed files with 9 additions and 1 deletions
  1. 9 1
      src/Tools/Utils.php

+ 9 - 1
src/Tools/Utils.php

@@ -197,7 +197,15 @@ class Utils
                 $factory = $factory->$methodName();
             }
         } else {
-            $factory = factory($modelName);
+            try {
+                $factory = factory($modelName);
+            } catch (\Throwable $e) {
+                if (Str::contains($e->getMessage(), "Call to undefined function Knuckles\Scribe\Tools\factory()")) {
+                    throw new \Exception("Couldn't find the Eloquent model factory. Did you add the HasFactory trait to your $modelName model?");
+                } else {
+                    throw $e;
+                }
+            }
             if (count($states)) {
                 $factory = $factory->states($states);
             }