{"id":"8ff89ed281c1bc34","repo":"laravel/framework","slug":"queueing-collections-with-multiple-model-connectio","errorCode":null,"errorMessage":"Queueing collections with multiple model connections is not supported.","messagePattern":"Queueing collections with multiple model connections is not supported\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Collection.php","lineNumber":916,"sourceCode":"\n    /**\n     * Get the connection of the entities being queued.\n     *\n     * @return string|null\n     *\n     * @throws \\LogicException\n     */\n    public function getQueueableConnection()\n    {\n        if ($this->isEmpty()) {\n            return;\n        }\n\n        $connection = $this->first()->getConnectionName();\n\n        $this->each(function ($model) use ($connection) {\n            if ($model->getConnectionName() !== $connection) {\n                throw new LogicException('Queueing collections with multiple model connections is not supported.');\n            }\n        });\n\n        return $connection;\n    }\n\n    /**\n     * Get the Eloquent query builder from the collection.\n     *\n     * @return \\Illuminate\\Database\\Eloquent\\Builder<TModel>\n     *\n     * @throws \\LogicException\n     */\n    public function toQuery()\n    {\n        $model = $this->first();\n\n        if (! $model) {","sourceCodeStart":898,"sourceCodeEnd":934,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Collection.php#L898-L934","documentation":"Collection::getQueueableConnection() walks every model and requires they all report the same connection name. This matters because the queue restoration uses a single connection name; mixed connections would silently restore from the wrong database.","triggerScenarios":"Queueing a collection whose models live on different database connections, e.g. collect([$userOnMysql, $userOnPgsql]) passed to a queued job, or a multi-tenant setup where some models were explicitly ->on('tenant_a') before being collected.","commonSituations":"Multi-database or read/write split setups; models reassigned with ->setConnection() or ->on(); merging results from read-replica and primary; sharding where models share a class but differ by connection.","solutions":["Normalize the connection before queueing: $models->each->setConnection($conn).","Dispatch separate jobs per connection group: $models->groupBy(fn ($m) => $m->getConnectionName()).","Pass identifiers (IDs + class + connection) and re-resolve inside the job handler."],"exampleFix":"// before\nSyncRecords::dispatch($records); // records span 'mysql' and 'pgsql'\n\n// after\n$records->groupBy(fn ($m) => $m->getConnectionName())\n    ->each(fn ($group, $conn) => SyncRecords::dispatch($group));","handlingStrategy":"validation","validationCode":"$conns = $collection->map(fn ($m) => $m->getConnectionName())->unique();\nif ($conns->count() > 1) {\n    throw new \\LogicException('Mixed connections: ' . $conns->implode(','));\n}","typeGuard":"function sharesOneConnection(\\Illuminate\\Database\\Eloquent\\Collection $c): bool\n{\n    return $c->map(fn ($m) => $m->getConnectionName())->unique()->count() <= 1;\n}","tryCatchPattern":"try {\n    $job = CloneThis::dispatch($collection);\n} catch (\\LogicException $e) {\n    if (str_contains($e->getMessage(), 'multiple model connections')) {\n        $collection->groupBy(fn ($m) => $m->getConnectionName())\n            ->each(fn ($group) => CloneThis::dispatch($group));\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Avoid using ->on() / ->setConnection() on models you intend to queue together.","Verify connections when building multi-tenant collections.","Dispatch per-connection groups in multi-database setups."],"tags":["eloquent","queue","collection","database-connection","serialization"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}