Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ protected function gatherKeysByType($type, $keyType)
? array_keys($this->dictionary[$type])
: array_map(function ($modelId) {
return (string) $modelId;
}, array_filter(array_keys($this->dictionary[$type])));
}, array_filter(array_keys($this->dictionary[$type]), static function ($modelId) {
return $modelId !== '';
}));
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/Database/DatabaseEloquentMorphToTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Tests\Database\stubs\TestEnum;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;

class DatabaseEloquentMorphToTest extends TestCase
{
Expand Down Expand Up @@ -68,6 +69,21 @@ public function __toString()
], $dictionary);
}

public function testGatherKeysByTypeKeepsStringZeroForStringKeyTypes()
{
$relation = $this->getRelation();

$relation->addEagerConstraints([
(object) ['morph_type' => 'morph_type_1', 'foreign_key' => '0'],
(object) ['morph_type' => 'morph_type_1', 'foreign_key' => ''],
]);

$method = new ReflectionMethod(MorphTo::class, 'gatherKeysByType');
$method->setAccessible(true);

$this->assertSame(['0'], $method->invoke($relation, 'morph_type_1', 'string'));
}

public function testMorphToWithDefault()
{
$relation = $this->getRelation()->withDefault();
Expand Down
Loading