diff --git a/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php b/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php index 0667966a..0b897509 100644 --- a/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php +++ b/src/Extracting/Strategies/UrlParameters/GetFromLaravelAPI.php @@ -69,11 +69,20 @@ public function __invoke(ExtractedEndpointData $endpointData, array $routeRules) $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) {