laravel/framework · warning · InvalidArgumentException
Collection given to whereMorphedTo method may not be empty.
Error message
Collection given to whereMorphedTo method may not be empty.
What it means
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.
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.
Example fix
// before
$tags = collect(); // empty
$query->whereMorphedTo('taggable', $tags); // InvalidArgumentException
// after
if ($tags->isNotEmpty()) {
$query->whereMorphedTo('taggable', $tags);
} Defensive patterns
Strategy: validation
Validate before calling
$models = \Illuminate\Support\Collection::wrap($model);
if ($models->isEmpty()) {
// nothing to constrain; skip or return empty result
return;
}
$query->whereMorphedTo('commentable', $models); Type guard
function isNonEmptyModelCollection(mixed $models): bool
{
return \Illuminate\Support\Collection::wrap($models)->isNotEmpty();
} Prevention
- 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().
When it happens
Trigger: Calling $query->whereMorphedTo('commentable', collect()) or ->whereMorphedTo('commentable', $emptyCollection). BaseCollection::wrap() wraps a single model or collection; if the result isEmpty(), the InvalidArgumentException fires.
Common situations: 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.
Related errors
- Collection given to whereNotMorphedTo method may not be…
- Collection given to whereAttachedTo method may not be empty.
- Collection given to whereBelongsTo method may not be empty.
- Unable to create query for empty collection.
- Add fillable property
AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11).
Data as JSON: /api/errors/278aec7758b65da3.
Report an issue: GitHub.
Appendix: source
Thrown at src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:644
if (is_null($model)) {
return $this->whereNull($relation->qualifyColumn($relation->getMorphType()), $boolean);
}
if (is_string($model)) {
$morphMap = Relation::morphMap();
if (! empty($morphMap) && in_array($model, $morphMap)) {
$model = array_search($model, $morphMap, true);
}
return $this->where($relation->qualifyColumn($relation->getMorphType()), $model, null, $boolean);
}
$models = BaseCollection::wrap($model);
if ($models->isEmpty()) {
throw new InvalidArgumentException('Collection given to whereMorphedTo method may not be empty.');
}
return $this->where(function ($query) use ($relation, $models) {
$models->groupBy(fn ($model) => $model->getMorphClass())->each(function ($models) use ($query, $relation) {
$query->orWhere(function ($query) use ($relation, $models) {
$query->where($relation->qualifyColumn($relation->getMorphType()), $models->first()->getMorphClass())
->whereIn($relation->qualifyColumn($relation->getForeignKeyName()), $models->map->getKey());
});
});
}, null, null, $boolean);
}
/**
* Add a not morph-to relationship condition to the query.
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
* @param \Illuminate\Database\Eloquent\Model|iterable<int, \Illuminate\Database\Eloquent\Model>|string $model
* @return $thisView on GitHub (pinned to e0f6eb3518)