Skip to content

Commit

Permalink
[11.x] Add Relation::getMorphAlias() (laravel#51809)
Browse files Browse the repository at this point in the history
* Add Relation::getMorphAlias

* Update src/Illuminate/Database/Eloquent/Relations/Relation.php

Co-authored-by: Caleb White <[email protected]>

---------

Co-authored-by: Taylor Otwell <[email protected]>
Co-authored-by: Caleb White <[email protected]>
  • Loading branch information
3 people authored Jun 17, 2024
1 parent 534f0d3 commit 9c19eb8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/Database/DatabaseEloquentRelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9c19eb8

Please sign in to comment.