[13.x] Fix Relation::getMorphAlias() for zero aliases#60818
Conversation
|
That might be in the database but how would you map a substitute for |
Just to clarify, For instance, in legacy schemas or highly optimized databases, developers often map types using non-sequential integers like this: Relation::morphMap([
0 => Admin::class,
10 => User::class,
], false);(Note: We use non-sequential keys like The interesting part is that the forward lookup already works perfectly: Relation::getMorphedModel('0'); // Returns: Admin::classHowever, the reverse lookup currently fails. Calling Since integer aliases are fully supported by the morph map API and |
|
No offense, but this sounds absolutely wild. Not sure if Laravel should encourage this. |
The Problem
When looking up a morph map alias via
Relation::getMorphAlias(), the method uses a shorthand ternary operator (?:) on the result ofarray_search():If a model is mapped to the integer
0(which is a common practice when using legacy databases or ultra-optimized numeric discriminators),array_search()correctly finds and returns0.However, because PHP evaluates
0as falsey, the shorthand ternary discards the alias and falls back to returning the full class name instead.The Solution
This PR replaces the loose ternary check with an explicit type check (
!== false) against the result ofarray_search(). This ensures that0is properly preserved and returned as a valid alias, making its behavior consistent with all other numeric/integer aliases (like1or10).