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

  1. Guard the call: if ($models->isNotEmpty()) { $query->whereMorphedTo('commentable', $models); }.
  2. Skip the query entirely or short-circuit to a known-empty result when the input collection is empty.
  3. 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

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


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 $this

View on GitHub (pinned to e0f6eb3518)