Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,10 @@ public function getMorphClass()
return array_search(static::class, $morphMap, true);
}

if (Relation::$tableNameAsMorphType) {
return $this->getTable();
}

return static::class;
}

Expand Down
17 changes: 17 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ abstract class Relation
*/
public static $morphMap = [];

/**
* Indicates if the morph relation type should default to table name.
*
* @var bool
*/
public static $tableNameAsMorphType = false;

/**
* Create a new relation instance.
*
Expand Down Expand Up @@ -341,6 +348,16 @@ public static function morphMap(array $map = null, $merge = true)
return static::$morphMap;
}

/**
* Changes the default morph type to table name.
*
* @return void
*/
public static function tableNameAsMorphType()
{
self::$tableNameAsMorphType = true;
}

/**
* Builds a table-keyed array from model class names.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,23 @@ public function testCorrectMorphClassIsReturned()
}
}

public function testCorrectMorphClassIsReturnedOnChangingDefault()
{
Relation::tableNameAsMorphType();
Relation::morphMap(['alias' => EloquentModelCamelStub::class]);
Relation::morphMap(['alias2' => 'AnotherModel']);
$model = new EloquentModelStub;
$model2 = new EloquentModelCamelStub;

try {
$this->assertEquals('stub', $model->getMorphClass());
$this->assertEquals('alias', $model2->getMorphClass());
} finally {
Relation::morphMap([], false);
Relation::$tableNameAsMorphType = false;
}
}

public function testHasManyCreatesProperRelation()
{
$model = new EloquentModelStub;
Expand Down