Why HasOneThrough does not prevent the sql when the farParent key is null? #59355
-
Index: illuminate/Database/Eloquent/Relations/HasOneThrough.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/illuminate/Database/Eloquent/Relations/HasOneThrough.php b/illuminate/Database/Eloquent/Relations/HasOneThrough.php
--- a/illuminate/Database/Eloquent/Relations/HasOneThrough.php (revision 244032c422b541f1df1c5e1bae2e020ae195f9a8)
+++ b/illuminate/Database/Eloquent/Relations/HasOneThrough.php (date 1774384471161)
@@ -19,7 +19,8 @@
*/
public function getResults()
{
- return $this->first() ?: $this->getDefaultFor($this->farParent);
+ return (null !== $this->farParent->{$this->localKey} ? $this->first() : null)
+ ?? $this->getDefaultFor($this->farParent);
}
/**
HasOneThrough is a child of HasmanyThrough: public function getResults()
{
return !is_null($this->farParent->{$this->localKey})
? $this->get()
: $this->related->newCollection();
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
|
Inconsistency between HasOneThrough and HasManyThrough HasManyThrough already prevents unnecessary queries: However, HasOneThrough currently lacks this check, leading to an redundant SQL execution that will always return empty results (or the default model) when the key is null. Proposed change for my side: |
Beta Was this translation helpful? Give feedback.
Laravel 12 has it already.