{"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":"error","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/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php#L669-L705","documentation":"whereNotMorphedTo() mirrors whereMorphedTo() with a NOT constraint. An empty model collection cannot define what to exclude, so it raises InvalidArgumentException to avoid generating a no-op (always-true) query that would silently return everything.","triggerScenarios":"Calling $query->whereNotMorphedTo('commentable', collect([])) (or an empty iterable), e.g. when excluding items from a user filter that came back empty.","commonSituations":"Optional 'exclude' filter that resolved to no IDs; empty result from a sub-query feeding whereNotMorphedTo; defensive code path that forgot to short-circuit on empty input.","solutions":["Skip the clause when empty: if ($exclude->isNotEmpty()) { $query->whereNotMorphedTo(...); }.","If exclusion of 'nothing' should be a no-op, structure the builder conditionally rather than passing empty.","Validate request inputs upstream so empty exclude lists never reach the query layer."],"exampleFix":"// before\n$query->whereNotMorphedTo('commentable', $excludedModels); // may be empty\n\n// after\nif ($excludedModels->isNotEmpty()) {\n    $query->whereNotMorphedTo('commentable', $excludedModels);\n}","handlingStrategy":"validation","validationCode":"$models = \\Illuminate\\Support\\Collection::wrap($model);\nif ($models->isEmpty()) {\n    // exclusion of nothing -> no clause needed\n    return $query;\n}\nreturn $query->whereNotMorphedTo($relation, $models);","typeGuard":"function hasExcludableMorphModels(mixed $model): bool\n{\n    return ! \\Illuminate\\Support\\Collection::wrap($model)->isEmpty();\n}","tryCatchPattern":"try {\n    return $query->whereNotMorphedTo($relation, $models);\n} catch (\\InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'may not be empty')) {\n        return $query;\n    }\n    throw $e;\n}","preventionTips":["Short-circuit the exclude branch when the collection is empty.","Validate 'exclude' request inputs before they reach the query layer.","Add a test for the empty-exclude case to lock the no-op semantics."],"tags":["eloquent","morph-query","validation","polymorphic"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}