{"record":{"id":"273c2c68bb4f21ba","repo":"laravel/framework","slug":"unable-to-create-query-for-collection-with-mixed-t","errorCode":null,"errorMessage":"Unable to create query for collection with mixed types.","messagePattern":"Unable to create query for collection with mixed types\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Collection.php","lineNumber":941,"sourceCode":"    /**\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) {\n            throw new LogicException('Unable to create query for empty collection.');\n        }\n\n        $class = get_class($model);\n\n        if ($this->contains(fn ($model) => ! $model instanceof $class)) {\n            throw new LogicException('Unable to create query for collection with mixed types.');\n        }\n\n        return $model->newModelQuery()->whereKey($this->modelKeys());\n    }\n}\n","sourceCodeStart":923,"sourceCodeEnd":947,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Collection.php#L923-L947","documentation":"Thrown by Eloquent\\Collection::toQuery() when the collection contains models of more than one concrete class. toQuery() derives the query from the first model's class and then uses whereKey(); it checks every element via contains(fn ($model) => ! $model instanceof $class) and rejects mixed types because a single query cannot target two tables.","triggerScenarios":"Calling toQuery() on a collection built from merge()/concat() of different model types, or on a polymorphic relation collection that holds more than one concrete model class. The contains() check fires on the second distinct class.","commonSituations":"Combining results from two models and then trying to update them together; a custom relation that returns mixed concrete types; storing base-class instances and subclass instances together.","solutions":["Split the collection by class with groupBy(fn ($m) => get_class($m)) and call toQuery() per group.","Rebuild the data so only one model type is present before calling toQuery().","Use separate per-type query/update calls instead of one combined query."],"exampleFix":"// before\n$collection = $users->concat($posts);\n$collection->toQuery()->delete(); // LogicException\n\n// after\n$collection->groupBy(fn ($m) => get_class($m))\n    ->each(fn ($group) => $group->toQuery()->delete());","handlingStrategy":"validation","validationCode":"$classes = $collection->map(fn ($m) => get_class($m))->unique();\nif ($classes->count() > 1) {\n    throw new \\LogicException('Collection mixes model types: ' . $classes->implode(', '));\n}\n$query = $collection->toQuery();","typeGuard":"function isHomogeneousByClass(\\Illuminate\\Support\\Collection $c): bool\n{\n    return $c->map(fn ($m) => get_class($m))->unique()->count() === 1;\n}","tryCatchPattern":null,"preventionTips":["Do not merge() / concat() different model types into a collection you will call toQuery() on.","Group by class before toQuery() when types legitimately vary.","Add a test covering the mixed-type branch."],"tags":["eloquent","collection","query-builder","polymorphism"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}