{"record":{"id":"278aec7758b65da3","repo":"laravel/framework","slug":"collection-given-to-wheremorphedto-method-may-not","errorCode":null,"errorMessage":"Collection given to whereMorphedTo method may not be empty.","messagePattern":"Collection given to whereMorphedTo method may not be empty\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"warning","filePath":"src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php","lineNumber":644,"sourceCode":"\n        if (is_null($model)) {\n            return $this->whereNull($relation->qualifyColumn($relation->getMorphType()), $boolean);\n        }\n\n        if (is_string($model)) {\n            $morphMap = Relation::morphMap();\n\n            if (! empty($morphMap) && in_array($model, $morphMap)) {\n                $model = array_search($model, $morphMap, true);\n            }\n\n            return $this->where($relation->qualifyColumn($relation->getMorphType()), $model, null, $boolean);\n        }\n\n        $models = BaseCollection::wrap($model);\n\n        if ($models->isEmpty()) {\n            throw new InvalidArgumentException('Collection given to whereMorphedTo method may not be empty.');\n        }\n\n        return $this->where(function ($query) use ($relation, $models) {\n            $models->groupBy(fn ($model) => $model->getMorphClass())->each(function ($models) use ($query, $relation) {\n                $query->orWhere(function ($query) use ($relation, $models) {\n                    $query->where($relation->qualifyColumn($relation->getMorphType()), $models->first()->getMorphClass())\n                        ->whereIn($relation->qualifyColumn($relation->getForeignKeyName()), $models->map->getKey());\n                });\n            });\n        }, null, null, $boolean);\n    }\n\n    /**\n     * Add a not morph-to relationship condition to the query.\n     *\n     * @param  \\Illuminate\\Database\\Eloquent\\Relations\\MorphTo<*, *>|string  $relation\n     * @param  \\Illuminate\\Database\\Eloquent\\Model|iterable<int, \\Illuminate\\Database\\Eloquent\\Model>|string  $model\n     * @return $this","sourceCodeStart":626,"sourceCodeEnd":662,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php#L626-L662","documentation":"Thrown by whereMorphedTo() when the collection (or single model wrapped into one) passed as the morph target is empty. whereMorphedTo builds morph_type + morph_id WHERE constraints grouped by getMorphClass(); with no models there is nothing to group or constrain, so Laravel rejects the empty input up front instead of producing a no-op or malformed query.","triggerScenarios":"Calling $query->whereMorphedTo('commentable', collect()) or ->whereMorphedTo('commentable', $emptyCollection). BaseCollection::wrap() wraps a single model or collection; if the result isEmpty(), the InvalidArgumentException fires.","commonSituations":"Passing a dynamically-built collection that happened to be empty (e.g. a filter returned no results); passing $user->permissions->pluck(...) where the pluck yields nothing; an edge case in a loop where one iteration has zero matches.","solutions":["Guard the call: if ($models->isNotEmpty()) { $query->whereMorphedTo('commentable', $models); }.","Skip the query entirely or short-circuit to a known-empty result when the input collection is empty.","Validate upstream that the collection is populated before reaching the query builder."],"exampleFix":"// before\n$tags = collect(); // empty\n$query->whereMorphedTo('taggable', $tags); // InvalidArgumentException\n\n// after\nif ($tags->isNotEmpty()) {\n    $query->whereMorphedTo('taggable', $tags);\n}","handlingStrategy":"validation","validationCode":"$models = \\Illuminate\\Support\\Collection::wrap($model);\nif ($models->isEmpty()) {\n    // nothing to constrain; skip or return empty result\n    return;\n}\n$query->whereMorphedTo('commentable', $models);","typeGuard":"function isNonEmptyModelCollection(mixed $models): bool\n{\n    return \\Illuminate\\Support\\Collection::wrap($models)->isNotEmpty();\n}","tryCatchPattern":null,"preventionTips":["Guard whereMorphedTo() with an isNotEmpty() check.","Validate upstream that exclusion/inclusion collections are populated.","Add a test for the empty-input path of any query using whereMorphedTo()."],"tags":["eloquent","polymorphic","query-builder","empty-state","validation"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}