diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index b5e8864f0937..cba4ca2afed0 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -501,6 +501,17 @@ public static function getMorphedModel($alias) return static::$morphMap[$alias] ?? null; } + /** + * Get the alias associated with a custom polymorphic class. + * + * @param string $className + * @return int|string|null + */ + public static function getMorphAlias(string $className) + { + return array_flip(static::$morphMap)[$className] ?? null; + } + /** * Handle dynamic method calls to the relationship. * diff --git a/tests/Database/DatabaseEloquentRelationTest.php b/tests/Database/DatabaseEloquentRelationTest.php index f1a8aab3fb11..47bd92710bcf 100755 --- a/tests/Database/DatabaseEloquentRelationTest.php +++ b/tests/Database/DatabaseEloquentRelationTest.php @@ -235,6 +235,14 @@ public function testSettingMorphMapWithNumericKeys() Relation::morphMap([], false); } + public function testGetMorphAlias() + { + Relation::morphMap(['user' => 'App\User']); + + $this->assertEquals('user', Relation::getMorphAlias('App\User')); + $this->assertNull(Relation::getMorphAlias('Does\Not\Exist')); + } + public function testWithoutRelations() { $original = new EloquentNoTouchingModelStub;