Skip to content

Commit 04955c4

Browse files
authored
Simplify codebase by using qualifyColumn helper method (#54187)
1 parent 7458750 commit 04955c4

File tree

7 files changed

+10
-18
lines changed

7 files changed

+10
-18
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,12 +1729,8 @@ protected function createSelectWithConstraint($name)
17291729
{
17301730
return [explode(':', $name)[0], static function ($query) use ($name) {
17311731
$query->select(array_map(static function ($column) use ($query) {
1732-
if (str_contains($column, '.')) {
1733-
return $column;
1734-
}
1735-
17361732
return $query instanceof BelongsToMany
1737-
? $query->getRelated()->getTable().'.'.$column
1733+
? $query->getRelated()->qualifyColumn($column)
17381734
: $column;
17391735
}, explode(',', explode(':', $name)[1])));
17401736
}];

src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
110110

111111
$localKey = $localKey ?: $this->getKeyName();
112112

113-
return $this->newHasOne($instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey);
113+
return $this->newHasOne($instance->newQuery(), $this, $instance->qualifyColumn($foreignKey), $localKey);
114114
}
115115

116116
/**
@@ -198,11 +198,9 @@ public function morphOne($related, $name, $type = null, $id = null, $localKey =
198198

199199
[$type, $id] = $this->getMorphs($name, $type, $id);
200200

201-
$table = $instance->getTable();
202-
203201
$localKey = $localKey ?: $this->getKeyName();
204202

205-
return $this->newMorphOne($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);
203+
return $this->newMorphOne($instance->newQuery(), $this, $instance->qualifyColumn($type), $instance->qualifyColumn($id), $localKey);
206204
}
207205

208206
/**
@@ -431,7 +429,7 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
431429
$localKey = $localKey ?: $this->getKeyName();
432430

433431
return $this->newHasMany(
434-
$instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey
432+
$instance->newQuery(), $this, $instance->qualifyColumn($foreignKey), $localKey
435433
);
436434
}
437435

@@ -527,11 +525,9 @@ public function morphMany($related, $name, $type = null, $id = null, $localKey =
527525
// get the table and create the relationship instances for the developers.
528526
[$type, $id] = $this->getMorphs($name, $type, $id);
529527

530-
$table = $instance->getTable();
531-
532528
$localKey = $localKey ?: $this->getKeyName();
533529

534-
return $this->newMorphMany($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey);
530+
return $this->newMorphMany($instance->newQuery(), $this, $instance->qualifyColumn($type), $instance->qualifyColumn($id), $localKey);
535531
}
536532

537533
/**

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,7 @@ protected function resolveChildRouteBindingQuery($childType, $value, $field)
21342134

21352135
if ($relationship instanceof HasManyThrough ||
21362136
$relationship instanceof BelongsToMany) {
2137-
$field = $relationship->getRelated()->getTable().'.'.$field;
2137+
$field = $relationship->getRelated()->qualifyColumn($field);
21382138
}
21392139

21402140
return $relationship instanceof Model

src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ public function get($columns = ['*'])
892892
protected function shouldSelect(array $columns = ['*'])
893893
{
894894
if ($columns == ['*']) {
895-
$columns = [$this->related->getTable().'.*'];
895+
$columns = [$this->related->qualifyColumn('*')];
896896
}
897897

898898
return array_merge($columns, $this->aliasedPivotColumns());

src/Illuminate/Database/Eloquent/Relations/Concerns/CanBeOneOfMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ public function qualifySubSelectColumn($column)
297297
*/
298298
protected function qualifyRelatedColumn($column)
299299
{
300-
return str_contains($column, '.') ? $column : $this->query->getModel()->getTable().'.'.$column;
300+
return $this->query->getModel()->qualifyColumn($column);
301301
}
302302

303303
/**

src/Illuminate/Database/Eloquent/Relations/HasOneOrManyThrough.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ public function cursorPaginate($perPage = null, $columns = ['*'], $cursorName =
501501
protected function shouldSelect(array $columns = ['*'])
502502
{
503503
if ($columns == ['*']) {
504-
$columns = [$this->related->getTable().'.*'];
504+
$columns = [$this->related->qualifyColumn('*')];
505505
}
506506

507507
return array_merge($columns, [$this->getQualifiedFirstKeyName().' as laravel_through_key']);

src/Illuminate/Database/Eloquent/Relations/MorphTo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ protected function getResultsByType($type)
162162
$whereIn = $this->whereInMethod($instance, $ownerKey);
163163

164164
return $query->{$whereIn}(
165-
$instance->getTable().'.'.$ownerKey, $this->gatherKeysByType($type, $instance->getKeyType())
165+
$instance->qualifyColumn($ownerKey), $this->gatherKeysByType($type, $instance->getKeyType())
166166
)->get();
167167
}
168168

0 commit comments

Comments
 (0)