{"record":{"id":"f76189c4e80c81e8","repo":"laravel/framework","slug":"collection-given-to-wherenotmorphedto-method-may-n","errorCode":null,"errorMessage":"Collection given to whereNotMorphedTo method may not be empty.","messagePattern":"Collection given to whereNotMorphedTo method may not be empty\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"warning","filePath":"src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php","lineNumber":687,"sourceCode":"            $relation = $this->getRelationWithoutConstraints($relation);\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->whereNot(fn ($query) => $query->whereNullSafeEquals(\n                $relation->qualifyColumn($relation->getMorphType()), $model\n            ), null, null, $boolean);\n        }\n\n        $models = BaseCollection::wrap($model);\n\n        if ($models->isEmpty()) {\n            throw new InvalidArgumentException('Collection given to whereNotMorphedTo method may not be empty.');\n        }\n\n        return $this->whereNot(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->whereNullSafeEquals($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 morph-to relationship condition to the query with an \"or where\" clause.\n     *\n     * @param  \\Illuminate\\Database\\Eloquent\\Relations\\MorphTo<*, *>|string  $relation\n     * @param  \\Illuminate\\Database\\Eloquent\\Model|iterable<int, \\Illuminate\\Database\\Eloquent\\Model>|string|null  $model\n     * @return $this","sourceCodeStart":669,"sourceCodeEnd":705,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php#L669-L705","documentation":"Thrown by whereNotMorphedTo() when the collection (or single model wrapped into one) passed as the morph target is empty. The method builds a whereNot group of morph_type/morph_id constraints; an empty collection would yield an empty group, so Laravel throws rather than emit a meaningless or always-true clause.","triggerScenarios":"Calling $query->whereNotMorphedTo('commentable', collect()) or ->whereNotMorphedTo('commentable', $emptyCollection). After BaseCollection::wrap(), an empty input triggers the isEmpty() guard.","commonSituations":"Passing a collection built from a filter/exclusion list that resolved empty (e.g. 'exclude these tags' where there are none to exclude); programmatically-built exclusion collections that are empty on the first run.","solutions":["Guard the call: if ($models->isNotEmpty()) { $query->whereNotMorphedTo('commentable', $models); }.","If the exclusion set is intentionally empty, omit the whereNotMorphedTo clause entirely.","Validate the collection is populated before constructing the query."],"exampleFix":"// before\n$excluded = collect(); // empty\n$query->whereNotMorphedTo('taggable', $excluded); // InvalidArgumentException\n\n// after\nif ($excluded->isNotEmpty()) {\n    $query->whereNotMorphedTo('taggable', $excluded);\n}","handlingStrategy":"validation","validationCode":"$models = \\Illuminate\\Support\\Collection::wrap($model);\nif ($models->isEmpty()) {\n    // exclusion set is empty; omit the whereNot clause\n    return;\n}\n$query->whereNotMorphedTo('commentable', $models);","typeGuard":"function isNonEmptyModelCollection(mixed $models): bool\n{\n    return \\Illuminate\\Support\\Collection::wrap($models)->isNotEmpty();\n}","tryCatchPattern":null,"preventionTips":["Guard whereNotMorphedTo() with an isNotEmpty() check.","Treat an empty exclusion set as 'no constraint needed' rather than passing it through.","Add a test for the empty-input path of any query using whereNotMorphedTo()."],"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"}