Skip to content

Commit cff895d

Browse files
authored
fix: use qualifyColumn rather than assuming format (#53559)
* feat: use model to qualify column * tests: update mock * refactor: use existing method
1 parent 365bef4 commit cff895d

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ public function addConstraints()
9696
// For belongs to relationships, which are essentially the inverse of has one
9797
// or has many relationships, we need to actually query on the primary key
9898
// of the related models matching on the foreign key that's on a parent.
99-
$table = $this->related->getTable();
99+
$key = $this->getQualifiedOwnerKeyName();
100100

101-
$this->query->where($table.'.'.$this->ownerKey, '=', $this->getForeignKeyFrom($this->child));
101+
$this->query->where($key, '=', $this->getForeignKeyFrom($this->child));
102102
}
103103
}
104104

@@ -108,7 +108,7 @@ public function addEagerConstraints(array $models)
108108
// We'll grab the primary key name of the related models since it could be set to
109109
// a non-standard name and not "id". We will then construct the constraint for
110110
// our eagerly loading query so it returns the proper models from execution.
111-
$key = $this->related->getTable().'.'.$this->ownerKey;
111+
$key = $this->getQualifiedOwnerKeyName();
112112

113113
$whereIn = $this->whereInMethod($this->related, $this->ownerKey);
114114

tests/Database/DatabaseEloquentBelongsToTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ protected function getRelation($parent = null, $keyType = 'int')
404404
$this->related->shouldReceive('getKeyType')->andReturn($keyType);
405405
$this->related->shouldReceive('getKeyName')->andReturn('id');
406406
$this->related->shouldReceive('getTable')->andReturn('relation');
407+
$this->related->shouldReceive('qualifyColumn')->andReturnUsing(fn (string $column) => "relation.{$column}");
407408
$this->builder->shouldReceive('getModel')->andReturn($this->related);
408409
$parent = $parent ?: new EloquentBelongsToModelStub;
409410

tests/Database/DatabaseEloquentMorphToTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ protected function getRelationAssociate($parent)
368368
$related = m::mock(Model::class);
369369
$related->shouldReceive('getKey')->andReturn(1);
370370
$related->shouldReceive('getTable')->andReturn('relation');
371+
$related->shouldReceive('qualifyColumn')->andReturnUsing(fn (string $column) => "relation.{$column}");
371372
$builder->shouldReceive('getModel')->andReturn($related);
372373

373374
return new MorphTo($builder, $parent, 'foreign_key', 'id', 'morph_type', 'relation');
@@ -378,8 +379,9 @@ public function getRelation($parent = null, $builder = null)
378379
$this->builder = $builder ?: m::mock(Builder::class);
379380
$this->builder->shouldReceive('where')->with('relation.id', '=', 'foreign.value');
380381
$this->related = m::mock(Model::class);
381-
$this->related->shouldReceive('getKeyName')->andReturn('id');
382+
$this->related->shouldReceive('getcolumn')->andReturn('id');
382383
$this->related->shouldReceive('getTable')->andReturn('relation');
384+
$this->related->shouldReceive('qualifyColumn')->andReturnUsing(fn (string $column) => "relation.{$column}");
383385
$this->builder->shouldReceive('getModel')->andReturn($this->related);
384386
$parent = $parent ?: new EloquentMorphToModelStub;
385387

0 commit comments

Comments
 (0)