Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[5.4] add getMorphedModel to Relation class #20244

Merged
merged 2 commits into from
Jul 25, 2017
Merged

[5.4] add getMorphedModel to Relation class #20244

merged 2 commits into from
Jul 25, 2017

Conversation

Lloople
Copy link
Contributor

@Lloople Lloople commented Jul 24, 2017

I couldn't found a way to do it with the current classes or methods. What this method does is to retrieve the model class that will be used before a related model is created. It's useful for example like this:

// app/Providers/AppServiceProvider.php

Relation::morphMap([
    'posts' => 'App\Post',
    'videos' => 'App\Video',
]);

// any place on your application

$morphedClass = Relation::getMorphedModel($comment->commentable_type); // posts to App\Post

$morphedModel = $morphedClass::createFromComment($comment); // did this way because Post and Video have different initial/default values

@taylorotwell taylorotwell merged commit f690eaf into laravel:5.4 Jul 25, 2017
@tomlankhorst
Copy link
Contributor

The inverse:

Relation::macro('morphAlias', function (string $class) {
  return array_search($class, Relation::$morphMap) ?: $class;
});```

@GrahamCampbell GrahamCampbell changed the title [5.5] add getMorphedModel to Relation class [5.4] add getMorphedModel to Relation class Apr 10, 2019
@ohnotnow
Copy link
Contributor

@tomlankhorst that's just saved me a lot of tinkering - thanks! :-)

@Lloople
Copy link
Contributor Author

Lloople commented Sep 17, 2019

@tomlankhorst It would be nice to have that on the framework by default. Have you tried creating a Pull Request?

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

Successfully merging this pull request may close these issues.

4 participants