{"record":{"id":"0fe203b767e27d26","repo":"crater-invoice-inc/crater","slug":"can-not-create-a-unique-slug","errorCode":null,"errorMessage":"Can not create a unique slug","messagePattern":"Can not create a unique slug","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"app/Space/helpers.php","lineNumber":179,"sourceCode":"\n    // Get any that could possibly be related.\n    // This cuts the queries down by doing it once.\n    $allSlugs = getRelatedSlugs($model, $slug, $id);\n\n    // If we haven't used it before then we are all good.\n    if (! $allSlugs->contains('slug', $slug)) {\n        return $slug;\n    }\n\n    // Just append numbers like a savage until we find not used.\n    for ($i = 1; $i <= 10; $i++) {\n        $newSlug = $slug.'_'.$i;\n        if (! $allSlugs->contains('slug', $newSlug)) {\n            return $newSlug;\n        }\n    }\n\n    throw new \\Exception('Can not create a unique slug');\n}\n\nfunction getRelatedSlugs($type, $slug, $id = 0)\n{\n    return CustomField::select('slug')->where('slug', 'like', $slug.'%')\n        ->where('model_type', $type)\n        ->where('id', '<>', $id)\n        ->get();\n}\n\nfunction respondJson($error, $message)\n{\n    return response()->json([\n        'error' => $error,\n        'message' => $message\n    ], 422);\n}\n","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/crater-invoice-inc/crater/blob/05d5ce26fdd8d9466009163444e944259bc0cc2a/app/Space/helpers.php#L161-L197","documentation":"The slug helper clean_slug throws after exhausting its 10 suffix attempts (_1 through _10): every candidate slug already exists for the model, typically because the same base slug was saved many times (e.g. repeated items named identically), so no unique slug can be derived.","triggerScenarios":"Thrown at app/Space/helpers.php:179 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Wrap the model save in try-catch and surface a 'please choose a different name' validation error","Increase the loop bound or switch to a random-suffix/timestamp fallback instead of a fixed 1-10 range","Use a unique index plus retry logic so the DB remains the source of truth for uniqueness"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"05d5ce26fdd8d9466009163444e944259bc0cc2a","analyzedAt":"2026-09-13T09:14:24.260Z","contentChangedAt":"2026-09-13T09:14:24.260Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}