diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index e3db55800aa1..925d80e9c681 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -1222,6 +1222,17 @@ public function setModel(Model $model) return $this; } + /** + * Qualify the given column name by the model's table. + * + * @param string $column + * @return string + */ + public function qualifyColumn($column) + { + return $this->model->qualifyColumn($column); + } + /** * Get the given macro by name. * diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index 11685f869ac3..cbb3ee648a43 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -249,6 +249,21 @@ public function forceFill(array $attributes) }); } + /** + * Qualify the given column name by the model's table. + * + * @param string $column + * @return string + */ + public function qualifyColumn($column) + { + if (Str::contains($column, '.')) { + return $column; + } + + return $this->getTable().'.'.$column; + } + /** * Remove the table name from a given key. * @@ -1206,7 +1221,7 @@ public function setKeyName($key) */ public function getQualifiedKeyName() { - return $this->getTable().'.'.$this->getKeyName(); + return $this->qualifyColumn($this->getKeyName()); } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php index 26c4fba061b3..7ba395bb7aff 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsTo.php @@ -253,7 +253,7 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, } return $query->select($columns)->whereColumn( - $this->getQualifiedForeignKey(), '=', $query->getModel()->getTable().'.'.$this->ownerKey + $this->getQualifiedForeignKey(), '=', $query->qualifyColumn($this->ownerKey) ); } @@ -327,7 +327,7 @@ public function getForeignKey() */ public function getQualifiedForeignKey() { - return $this->child->getTable().'.'.$this->foreignKey; + return $this->child->qualifyColumn($this->foreignKey); } /** @@ -347,7 +347,7 @@ public function getOwnerKey() */ public function getQualifiedOwnerKeyName() { - return $this->related->getTable().'.'.$this->ownerKey; + return $this->related->qualifyColumn($this->ownerKey); } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 76f5b3693238..dd2863d20ae2 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -959,7 +959,7 @@ public function getQualifiedRelatedPivotKeyName() */ public function getQualifiedParentKeyName() { - return $this->parent->getTable().'.'.$this->parentKey; + return $this->parent->qualifyColumn($this->parentKey); } /** diff --git a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php index 9eb58daa40f0..408e42fc7a50 100644 --- a/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php @@ -125,7 +125,7 @@ protected function performJoin(Builder $query = null) */ public function getQualifiedParentKeyName() { - return $this->parent->getTable().'.'.$this->secondLocalKey; + return $this->parent->qualifyColumn($this->secondLocalKey); } /** @@ -495,7 +495,7 @@ public function getQualifiedFarKeyName() */ public function getQualifiedFirstKeyName() { - return $this->throughParent->getTable().'.'.$this->firstKey; + return $this->throughParent->qualifyColumn($this->firstKey); } /** @@ -505,7 +505,7 @@ public function getQualifiedFirstKeyName() */ public function getQualifiedForeignKeyName() { - return $this->related->getTable().'.'.$this->secondKey; + return $this->related->qualifyColumn($this->secondKey); } /** @@ -515,6 +515,6 @@ public function getQualifiedForeignKeyName() */ public function getQualifiedLocalKeyName() { - return $this->farParent->getTable().'.'.$this->localKey; + return $this->farParent->qualifyColumn($this->localKey); } } diff --git a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php index 01c9a64ceb16..54850fa33b24 100755 --- a/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php @@ -396,7 +396,7 @@ public function getParentKey() */ public function getQualifiedParentKeyName() { - return $this->parent->getTable().'.'.$this->localKey; + return $this->parent->qualifyColumn($this->localKey); } /** diff --git a/src/Illuminate/Database/Eloquent/SoftDeletes.php b/src/Illuminate/Database/Eloquent/SoftDeletes.php index 879dad278cd6..74ba0622bf65 100644 --- a/src/Illuminate/Database/Eloquent/SoftDeletes.php +++ b/src/Illuminate/Database/Eloquent/SoftDeletes.php @@ -164,6 +164,6 @@ public function getDeletedAtColumn() */ public function getQualifiedDeletedAtColumn() { - return $this->getTable().'.'.$this->getDeletedAtColumn(); + return $this->qualifyColumn($this->getDeletedAtColumn()); } } diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 00ce19b793b7..9011fadbd4f6 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Collection as BaseCollection; +use Illuminate\Database\Query\Builder as BaseBuilder; class DatabaseEloquentBuilderTest extends TestCase { @@ -126,6 +127,16 @@ public function testFirstMethod() $this->assertEquals('bar', $result); } + public function testQualifyColumn() + { + $builder = new Builder(m::mock(BaseBuilder::class)); + $builder->shouldReceive('from')->with('stub'); + + $builder->setModel(new EloquentModelStub); + + $this->assertEquals('stub.column', $builder->qualifyColumn('column')); + } + public function testGetMethodLoadsModelsAndHydratesEagerRelations() { $builder = m::mock('Illuminate\Database\Eloquent\Builder[getModels,eagerLoadRelations]', [$this->getMockQueryBuilder()]); @@ -366,7 +377,7 @@ public function testPluckWithoutModelGetterJustReturnTheAttributesFoundInDatabas public function testLocalMacrosAreCalledOnBuilder() { unset($_SERVER['__test.builder']); - $builder = new \Illuminate\Database\Eloquent\Builder(new \Illuminate\Database\Query\Builder( + $builder = new Builder(new BaseBuilder( m::mock('Illuminate\Database\ConnectionInterface'), m::mock('Illuminate\Database\Query\Grammars\Grammar'), m::mock('Illuminate\Database\Query\Processors\Processor') @@ -987,7 +998,7 @@ protected function getMockModel() protected function getMockQueryBuilder() { - $query = m::mock('Illuminate\Database\Query\Builder'); + $query = m::mock(BaseBuilder::class); $query->shouldReceive('from')->with('foo_table'); return $query; diff --git a/tests/Database/DatabaseEloquentHasOneTest.php b/tests/Database/DatabaseEloquentHasOneTest.php index 1f88a6deda85..d345be740e08 100755 --- a/tests/Database/DatabaseEloquentHasOneTest.php +++ b/tests/Database/DatabaseEloquentHasOneTest.php @@ -175,7 +175,7 @@ public function testRelationCountQueryCanBeBuilt() $builder->shouldReceive('getQuery')->once()->andReturn($parentQuery); $builder->shouldReceive('select')->once()->with(m::type('Illuminate\Database\Query\Expression'))->andReturnSelf(); - $relation->getParent()->shouldReceive('getTable')->andReturn('table'); + $relation->getParent()->shouldReceive('qualifyColumn')->andReturn('table.id'); $builder->shouldReceive('whereColumn')->once()->with('table.id', '=', 'table.foreign_key')->andReturn($baseQuery); $baseQuery->shouldReceive('setBindings')->once()->with([], 'select'); diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index adf821c2c855..2681cfb3a07e 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -866,6 +866,13 @@ public function testFillable() $this->assertEquals('bar', $model->age); } + public function testQualifyColumn() + { + $model = new EloquentModelStub; + + $this->assertEquals('stub.column', $model->qualifyColumn('column')); + } + public function testForceFillMethodFillsGuardedAttributes() { $model = (new EloquentModelSaveStub)->forceFill(['id' => 21]);