Skip to content

Commit 7dca817

Browse files
committed
Fix empty morph to relationship
1 parent f9bd8a1 commit 7dca817

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $otherKe
5353
parent::__construct($query, $parent, $foreignKey, $otherKey, $relation);
5454
}
5555

56+
/**
57+
* Get the results of the relationship.
58+
*
59+
* @return mixed
60+
*/
61+
public function getResults()
62+
{
63+
if ( ! $this->otherKey) return;
64+
65+
return $this->query->first();
66+
}
67+
5668
/**
5769
* Set the constraints for an eager load of the relation.
5870
*

tests/Database/DatabaseEloquentIntegrationTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ public function testBasicMorphManyRelationship()
324324
}
325325

326326

327+
public function testEmptyMorphToRelationship()
328+
{
329+
$photo = EloquentTestPhoto::create(['name' => 'Avatar 1']);
330+
331+
$this->assertNull($photo->imageable);
332+
}
333+
334+
327335
public function testMultiInsertsWithDifferentValues()
328336
{
329337
$date = '1970-01-01';

0 commit comments

Comments
 (0)