{"record":{"id":"6015b2c1abe5cb6a","repo":"mongodb/laravel-mongodb","slug":"the-mongodb-scout-collection-s-s-must-use-a-different","errorCode":null,"errorMessage":"The MongoDB Scout collection \"%s.%s\" must use a different collection from the collection name of the model \"%s\". Set the \"scout.prefix\" configuration or use a distinct MongoDB database","messagePattern":"The MongoDB Scout collection \"(.+?)\\.(.+?)\" must use a different collection from the collection name of the model \"(.+?)\"\\. Set the \"scout\\.prefix\" configuration or use a distinct MongoDB database","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Scout/ScoutEngine.php","lineNumber":515,"sourceCode":"        return $this->database->selectCollection($model->searchableAs());\n    }\n\n    /** Get the MongoDB collection used to index the provided model */\n    private function getIndexableCollection(Model|EloquentCollection $model): MongoDBCollection\n    {\n        if ($model instanceof EloquentCollection) {\n            $model = $model->first();\n        }\n\n        assert($model instanceof Model);\n        assert(method_exists($model, 'indexableAs'), sprintf('Model \"%s\" must use \"%s\" trait', $model::class, Searchable::class));\n\n        if (\n            $model->getConnection() instanceof Connection\n            && $model->getConnection()->getDatabaseName() === $this->database->getDatabaseName()\n            && $model->getTable() === $model->indexableAs()\n        ) {\n            throw new LogicException(sprintf('The MongoDB Scout collection \"%s.%s\" must use a different collection from the collection name of the model \"%s\". Set the \"scout.prefix\" configuration or use a distinct MongoDB database', $this->database->getDatabaseName(), $model->indexableAs(), $model::class));\n        }\n\n        return $this->database->selectCollection($model->indexableAs());\n    }\n\n    private static function serialize(mixed $value): mixed\n    {\n        if ($value instanceof DateTimeInterface) {\n            return new UTCDateTime($value);\n        }\n\n        if ($value instanceof Serializable || ! is_iterable($value)) {\n            return $value;\n        }\n\n        // Convert Laravel Collections and other Iterators to arrays\n        if ($value instanceof Traversable) {\n            $value = iterator_to_array($value);","sourceCodeStart":497,"sourceCodeEnd":533,"githubUrl":"https://github.com/mongodb/laravel-mongodb/blob/0634653039468ceb0268a69192bdac64469ed043/src/Scout/ScoutEngine.php#L497-L533","documentation":"The Scout engine must store search documents in a collection distinct from the model's own MongoDB collection; otherwise indexing would overwrite application data. getIndexableCollection() throws a LogicException when the model uses the same MongoDB connection/database and its table equals indexableAs() (no prefix separation).","triggerScenarios":"Calling update(), delete(), or flush() on a searchable model whose connection is a MongoDB Connection, whose database name equals the Scout engine's database, and whose table === indexableAs() (i.e. scout.prefix is empty).","commonSituations":"Fresh setups where scout.prefix was never configured; models whose searchIndexableAs/table naming collides; pointing Scout and the app at the same database without a prefix.","solutions":["Set the 'scout.prefix' config value (e.g. 'search_') so indexed collections are named differently","Point the Scout engine at a distinct MongoDB database","Change the model's table or searchableAs() so it differs from indexableAs()","Use a separate MongoDB connection for the Scout engine"],"exampleFix":"// before\n// config/scout.php: 'prefix' => env('SCOUT_PREFIX', '')\n// after\n'scout.prefix' => env('SCOUT_PREFIX', 'search_') // e.g. SCOUT_PREFIX=search_ in .env","handlingStrategy":"validation","validationCode":"$conn = $model->getConnection();\nif ($conn instanceof \\MongoDB\\Laravel\\Connection\n    && $conn->getDatabaseName() === config('scout.mongodb.database')\n    && $model->getTable() === $model->indexableAs()) {\n    throw new \\LogicException('Set scout.prefix or use a distinct database before indexing.');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $model->searchable();\n} catch (\\LogicException $e) {\n    if (str_contains($e->getMessage(), 'must use a different collection')) {\n        logger()->error('Configure scout.prefix or a separate Scout database.');\n    }\n    throw $e;\n}","preventionTips":["Set SCOUT_PREFIX in .env for every MongoDB Scout deployment","Point Scout at a dedicated MongoDB database separate from application data","Add a boot-time assertion that indexableAs() differs from the model table"],"tags":["php","laravel-scout","configuration","data-safety"],"backgroundTag":"missing-config-value","analyzedSha":"0634653039468ceb0268a69192bdac64469ed043","analyzedAt":"2026-09-15T02:56:37.067Z","contentChangedAt":"2026-09-15T02:56:37.067Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}