laravel/framework · warning · InvalidArgumentException

Collection given to whereNotMorphedTo method may not be…

Error message

Collection given to whereNotMorphedTo method may not be empty.

What it means

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.

Solutions

  1. Guard the call: if ($models->isNotEmpty()) { $query->whereNotMorphedTo('commentable', $models); }.
  2. If the exclusion set is intentionally empty, omit the whereNotMorphedTo clause entirely.
  3. Validate the collection is populated before constructing the query.

Example fix

// before
$excluded = collect(); // empty
$query->whereNotMorphedTo('taggable', $excluded); // InvalidArgumentException

// after
if ($excluded->isNotEmpty()) {
    $query->whereNotMorphedTo('taggable', $excluded);
}
Defensive patterns

Strategy: validation

Validate before calling

$models = \Illuminate\Support\Collection::wrap($model);
if ($models->isEmpty()) {
    // exclusion set is empty; omit the whereNot clause
    return;
}
$query->whereNotMorphedTo('commentable', $models);

Type guard

function isNonEmptyModelCollection(mixed $models): bool
{
    return \Illuminate\Support\Collection::wrap($models)->isNotEmpty();
}

Prevention

When it happens

Trigger: Calling $query->whereNotMorphedTo('commentable', collect()) or ->whereNotMorphedTo('commentable', $emptyCollection). After BaseCollection::wrap(), an empty input triggers the isEmpty() guard.

Common situations: 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.

Related errors


AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11). Data as JSON: /api/errors/f76189c4e80c81e8. Report an issue: GitHub.

Appendix: source

Thrown at src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:687

            $relation = $this->getRelationWithoutConstraints($relation);
        }

        if (is_string($model)) {
            $morphMap = Relation::morphMap();

            if (! empty($morphMap) && in_array($model, $morphMap)) {
                $model = array_search($model, $morphMap, true);
            }

            return $this->whereNot(fn ($query) => $query->whereNullSafeEquals(
                $relation->qualifyColumn($relation->getMorphType()), $model
            ), null, null, $boolean);
        }

        $models = BaseCollection::wrap($model);

        if ($models->isEmpty()) {
            throw new InvalidArgumentException('Collection given to whereNotMorphedTo method may not be empty.');
        }

        return $this->whereNot(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->whereNullSafeEquals($relation->qualifyColumn($relation->getMorphType()), $models->first()->getMorphClass())
                        ->whereIn($relation->qualifyColumn($relation->getForeignKeyName()), $models->map->getKey());
                });
            });
        }, null, null, $boolean);
    }

    /**
     * Add a morph-to relationship condition to the query with an "or where" clause.
     *
     * @param  \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string  $relation
     * @param  \Illuminate\Database\Eloquent\Model|iterable<int, \Illuminate\Database\Eloquent\Model>|string|null  $model
     * @return $this

View on GitHub (pinned to e0f6eb3518)