Browse Source

Merge pull request #409 from YannikFirre/patch-2

Fetch URL parameter examples from database
Shalvah 3 years ago
parent
commit
887d895944
1 changed files with 14 additions and 5 deletions
  1. 14 5
      src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php

+ 14 - 5
src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php

@@ -69,11 +69,20 @@ class GetFromLaravelAPI extends Strategy
                     $type = $this->normalizeTypeName($typeName);
                     $parameters[$paramName]['type'] = $type;
 
-                    // If the user explicitly set a `where()` constraint, use that to refine examples
-                    $parameterRegex = $endpointData->route->wheres[$paramName] ?? null;
-                    $example = $parameterRegex
-                        ? $this->castToType($this->getFaker()->regexify($parameterRegex), $type)
-                        : $this->generateDummyValue($type);
+                    // Try to fetch an example ID from the database
+                    try {
+                        $example = $argumentInstance::first()->id ?? null;
+                    } catch (\Throwable $e) {
+                        $example = null;
+                    }
+
+                    if ($example === null) {
+                        // If the user explicitly set a `where()` constraint, use that to refine examples
+                        $parameterRegex = $endpointData->route->wheres[$paramName] ?? null;
+                        $example = $parameterRegex
+                            ? $this->castToType($this->getFaker()->regexify($parameterRegex), $type)
+                            : $this->generateDummyValue($type);
+                    }
                     $parameters[$paramName]['example'] = $example;
                 }
             } catch (\Throwable $e) {