Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[9.x] Improve getter for morphed model alias #43487

Closed
wants to merge 13 commits into from
Closed

[9.x] Improve getter for morphed model alias #43487

wants to merge 13 commits into from

Conversation

sawirricardo
Copy link

@sawirricardo sawirricardo commented Jul 31, 2022

Background, I uses polymorphic models heavily in apps, which at times requires to do something like

Relation::enforceMorphMap(['user' => 'App\Models\User']);

Model::query()->whereModelType('user');

now, what if we updated the morph map from 'user' to 'other_user'?

Of couse we can imagine how many code we need to change.

// current way
User::make()->getMorphClass(); // we can do this better
User::morphAlias(); // 😍 returns 'other_user'
Relation::getMorphedModelAlias('App\Models\User') // 👍 also returns 'other_user'

Of course, suggestions are welcomed

@sawirricardo sawirricardo marked this pull request as draft July 31, 2022 13:30
@sawirricardo sawirricardo changed the title [9.x] Add getter for morphed model key [9.x] Improve getter for morphed model key Jul 31, 2022
@sawirricardo sawirricardo changed the title [9.x] Improve getter for morphed model key [9.x] Improve getter for morphed model alias Jul 31, 2022
@sawirricardo sawirricardo marked this pull request as ready for review July 31, 2022 13:59
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

@JamesHemery
Copy link
Contributor

JamesHemery commented Dec 24, 2022

Hi @taylorotwell,

What do you think about just adding this method on HasRelationships trait ?

    public static function morphAlias()
    {
        return (new static)->getMorphClass();
    }

It's a small DX improvement.

I also thought of changing the visibility of getMorphClass as protected, so that it could be called by __call and __callStatic from Model, BUT the method is mocked in several places and this would be a major change.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants