laravel/framework · error · LogicException
Unable to create query for empty collection.
Error message
Unable to create query for empty collection.
What it means
Collection::toQuery() builds a WHERE id IN (...) query against the first model's query builder. With an empty collection there is no first model, so it cannot know which table or class to query against and raises a LogicException.
Source
Thrown at src/Illuminate/Database/Eloquent/Collection.php:935
}
});
return $connection;
}
/**
* Get the Eloquent query builder from the collection.
*
* @return \Illuminate\Database\Eloquent\Builder<TModel>
*
* @throws \LogicException
*/
public function toQuery()
{
$model = $this->first();
if (! $model) {
throw new LogicException('Unable to create query for empty collection.');
}
$class = get_class($model);
if ($this->contains(fn ($model) => ! $model instanceof $class)) {
throw new LogicException('Unable to create query for collection with mixed types.');
}
return $model->newModelQuery()->whereKey($this->modelKeys());
}
}
View on GitHub (pinned to bd6b5437e6)
Solutions
- Guard with isEmpty(): if ($coll->isNotEmpty()) { $coll->toQuery(); }.
- Re-query from the model class instead: SomeModel::whereKey($ids)->... when you only have IDs.
- Return early or skip the branch when the upstream query produced no rows.
Example fix
// before
$query = $users->toQuery(); // $users may be empty
// after
if ($users->isNotEmpty()) {
$query = $users->toQuery();
} Defensive patterns
Strategy: validation
Validate before calling
if ($collection->isEmpty()) {
// skip or build query from the model class instead
return SomeModel::query()->whereRaw('1=0');
}
return $collection->toQuery(); Type guard
function canBuildQuery(\Illuminate\Support\Collection $c): bool
{
return $c->isNotEmpty();
} Try / catch
try {
return $collection->toQuery();
} catch (\LogicException $e) {
if (str_contains($e->getMessage(), 'empty collection')) {
return $fallbackBuilder ?? null;
}
throw $e;
} Prevention
- Always check isNotEmpty() before ->toQuery().
- Prefer operating on IDs via Model::whereKey($ids) when collection provenance is uncertain.
- Write a test for the empty-collection branch.
When it happens
Trigger: Calling $collection->toQuery() (or methods that delegate to it) when the collection has no elements, e.g. SomeModel::where(...)->get()->toQuery() after a no-row result.
Common situations: Chaining toQuery() directly after get() without a count check; helper code that assumes a non-empty result from a search/filter step; refactor that removed the guard clause.
Related errors
- Unable to create query for collection with mixed types.
- Collection given to whereBelongsTo method may not be empty.
- Collection given to whereAttachedTo method may not be empty.
- The chunkById operation was aborted because the [{$alias}] c
- No record found for the given query.
AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06).
Data as JSON: /data/errors/5df98d4c7e01e9a4.json.
Report an issue: GitHub.