Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Prevent unnecessary relationship queries #26992

Merged
merged 1 commit into from
Dec 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey
*/
public function getResults()
{
if (is_null($this->child->{$this->foreignKey})) {
return $this->getDefaultFor($this->parent);
}

return $this->query->first() ?: $this->getDefaultFor($this->parent);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,9 @@ public function firstOrFail($columns = ['*'])
*/
public function getResults()
{
return $this->get();
return ! is_null($this->parent->{$this->parentKey})
? $this->get()
: $this->related->newCollection();
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class HasMany extends HasOneOrMany
*/
public function getResults()
{
return $this->query->get();
return ! is_null($this->getParentKey())
? $this->query->get()
: $this->related->newCollection();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ public function findOrFail($id, $columns = ['*'])
*/
public function getResults()
{
return $this->get();
return ! is_null($this->farParent->{$this->localKey})
? $this->get()
: $this->related->newCollection();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class HasOne extends HasOneOrMany
*/
public function getResults()
{
if (is_null($this->getParentKey())) {
return $this->getDefaultFor($this->parent);
}

return $this->query->first() ?: $this->getDefaultFor($this->parent);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/MorphMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class MorphMany extends MorphOneOrMany
*/
public function getResults()
{
return $this->query->get();
return ! is_null($this->getParentKey())
? $this->query->get()
: $this->related->newCollection();
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class MorphOne extends MorphOneOrMany
*/
public function getResults()
{
if (is_null($this->getParentKey())) {
return $this->getDefaultFor($this->parent);
}

return $this->query->first() ?: $this->getDefaultFor($this->parent);
}

Expand Down
10 changes: 0 additions & 10 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,6 @@ protected function buildDictionary(Collection $models)
}
}

/**
* Get the results of the relationship.
*
* @return mixed
*/
public function getResults()
{
return $this->ownerKey ? parent::getResults() : null;
}

/**
* Get the results of the relationship.
*
Expand Down