diff --git a/src/Illuminate/Database/Eloquent/Relations/Pivot.php b/src/Illuminate/Database/Eloquent/Relations/Pivot.php index 40e93f779e14..1043c1ccd0cf 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Pivot.php +++ b/src/Illuminate/Database/Eloquent/Relations/Pivot.php @@ -214,7 +214,7 @@ public function hasTimestampAttributes() */ public function getCreatedAtColumn() { - return $this->pivotParent->getCreatedAtColumn(); + return ($this->pivotParent) ? $this->pivotParent->getCreatedAtColumn() : parent::getCreatedAtColumn(); } /** @@ -224,7 +224,7 @@ public function getCreatedAtColumn() */ public function getUpdatedAtColumn() { - return $this->pivotParent->getUpdatedAtColumn(); + return ($this->pivotParent) ? $this->pivotParent->getUpdatedAtColumn() : parent::getUpdatedAtColumn(); } /** diff --git a/tests/Database/DatabaseEloquentPivotTest.php b/tests/Database/DatabaseEloquentPivotTest.php index fefabe85b999..d1004125058e 100755 --- a/tests/Database/DatabaseEloquentPivotTest.php +++ b/tests/Database/DatabaseEloquentPivotTest.php @@ -4,6 +4,7 @@ use Mockery as m; use PHPUnit\Framework\TestCase; +use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\Pivot; class DatabaseEloquentPivotTest extends TestCase @@ -119,6 +120,29 @@ public function testPivotModelTableNameIsSingular() $this->assertEquals('pivot', $pivot->getTable()); } + + public function testPivotModelWithParentReturnsParentsTimestampColumns() + { + $parent = m::mock('Illuminate\Database\Eloquent\Model'); + $parent->shouldReceive('getCreatedAtColumn')->andReturn('parent_created_at'); + $parent->shouldReceive('getUpdatedAtColumn')->andReturn('parent_updated_at'); + + $pivotWithParent = new Pivot(); + $pivotWithParent->pivotParent = $parent; + + $this->assertEquals('parent_created_at', $pivotWithParent->getCreatedAtColumn()); + $this->assertEquals('parent_updated_at', $pivotWithParent->getUpdatedAtColumn()); + } + + public function testPivotModelWithoutParentReturnsModelTimestampColumns() + { + $model = new DummyModel(); + + $pivotWithoutParent = new Pivot(); + + $this->assertEquals($model->getCreatedAtColumn(), $pivotWithoutParent->getCreatedAtColumn()); + $this->assertEquals($model->getUpdatedAtColumn(), $pivotWithoutParent->getUpdatedAtColumn()); + } } class DatabaseEloquentPivotTestDateStub extends \Illuminate\Database\Eloquent\Relations\Pivot @@ -152,3 +176,7 @@ class DatabaseEloquentPivotTestJsonCastStub extends \Illuminate\Database\Eloquen 'foo' => 'json', ]; } + +class DummyModel extends Model +{ +}