diff --git a/src/Execution/BatchLoader/RelationBatchLoader.php b/src/Execution/BatchLoader/RelationBatchLoader.php index 1d9cc8b16..8985e26ba 100644 --- a/src/Execution/BatchLoader/RelationBatchLoader.php +++ b/src/Execution/BatchLoader/RelationBatchLoader.php @@ -49,13 +49,13 @@ public function load(Model $model): Deferred $this->resolve(); } - return $this->results[$modelKey]; + return $this->results[$modelKey] ?? null; }); } protected function resolve(): void { - $parentModels = new EloquentCollection($this->parents); + $parentModels = (new EloquentCollection($this->parents))->filter(fn (Model $model): bool => $model->exists); // Monomorphize the models to simplify eager loading relations onto them $parentsGroupedByClass = $parentModels->groupBy(