{"id":"fa847b2575e82765","repo":"laravel/framework","slug":"queueing-collections-with-multiple-model-types-is","errorCode":null,"errorMessage":"Queueing collections with multiple model types is not supported.","messagePattern":"Queueing collections with multiple model types is not supported\\.","errorType":"exception","errorClass":"LogicException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Collection.php","lineNumber":841,"sourceCode":"\n    /**\n     * Get the type of the entities being queued.\n     *\n     * @return string|null\n     *\n     * @throws \\LogicException\n     */\n    public function getQueueableClass()\n    {\n        if ($this->isEmpty()) {\n            return;\n        }\n\n        $class = $this->getQueueableModelClass($this->first());\n\n        $this->each(function ($model) use ($class) {\n            if ($this->getQueueableModelClass($model) !== $class) {\n                throw new LogicException('Queueing collections with multiple model types is not supported.');\n            }\n        });\n\n        return $class;\n    }\n\n    /**\n     * Get the queueable class name for the given model.\n     *\n     * @param  \\Illuminate\\Database\\Eloquent\\Model  $model\n     * @return string\n     */\n    protected function getQueueableModelClass($model)\n    {\n        return method_exists($model, 'getQueueableClassName')\n            ? $model->getQueueableClassName()\n            : get_class($model);\n    }","sourceCodeStart":823,"sourceCodeEnd":859,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Collection.php#L823-L859","documentation":"Eloquent's Collection::getQueueableClass() iterates every model when serializing a collection for the queue and requires them all to share the same class. If any model differs from the first, a LogicException is raised because PHP's queue payload can only describe one type per collection.","triggerScenarios":"Dispatching a queued job that receives an Eloquent Collection containing more than one model class, e.g. collect([new Post, new Comment])->... dispatched to a job, or passing $post->media (mixing Photo and Video models without a shared base morph class) to a Bus::dispatchToQueue job.","commonSituations":"Polymorphic collections built from union of multiple query results; merging related models of different types into one collection before queueing; using a base class that getQueueableClassName() does not collapse to a single class.","solutions":["Split the collection by class and dispatch one job per type.","Eager-load relations so each queued collection contains only one concrete type.","Pass only model keys (->modelKeys()) into the job and re-fetch inside the handler instead of serializing the collection."],"exampleFix":"// before\nProcessMedia::dispatch($post->media); // mixes Photo + Video -> throws\n\n// after\n$post->media->groupBy(fn ($m) => get_class($m))\n    ->each(fn ($group) => ProcessMedia::dispatch($group));","handlingStrategy":"validation","validationCode":"$classes = $collection->map(fn ($m) => get_class($m))->unique();\nif ($classes->count() > 1) {\n    throw new \\LogicException('Refusing to queue mixed-type collection: ' . $classes->implode(','));\n}\n// safe to dispatch","typeGuard":"function isHomogeneousType(\\Illuminate\\Support\\Collection $c): bool\n{\n    return $c->map(fn ($m) => get_class($m))->unique()->count() <= 1;\n}","tryCatchPattern":"try {\n    ProcessMedia::dispatch($collection);\n} catch (\\LogicException $e) {\n    if (str_contains($e->getMessage(), 'multiple model types')) {\n        $collection->groupBy(fn ($m) => get_class($m))\n            ->each(fn ($group) => ProcessMedia::dispatch($group));\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Pass only model keys to jobs and re-fetch inside the handler to avoid serialization constraints.","Group collections by class before queueing.","Assert in tests that queued payloads contain a single model type."],"tags":["eloquent","queue","collection","serialization"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}