Skip to content

Commit 8528d04

Browse files
authored
[11.x] Use getQualifiedOwnerKeyName in relations (#53573)
* feat: use model to qualify column * tests: update mock * refactor: use existing method * fix: handle null value * tests: update mocks * chore: remove duplicates * chore: revert unnecessary change
1 parent c272db4 commit 8528d04

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

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

+7-3
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

@@ -344,6 +344,10 @@ public function getOwnerKeyName()
344344
*/
345345
public function getQualifiedOwnerKeyName()
346346
{
347+
if (is_null($this->ownerKey)) {
348+
return '';
349+
}
350+
347351
return $this->related->qualifyColumn($this->ownerKey);
348352
}
349353

tests/Database/DatabaseEloquentBelongsToTest.php

+1
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

+2
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');
@@ -380,6 +381,7 @@ public function getRelation($parent = null, $builder = null)
380381
$this->related = m::mock(Model::class);
381382
$this->related->shouldReceive('getKeyName')->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)