[Request] Add Polymorphic Many-to-Many relationships
I've been working on an implementation for this a few months back but haven't really had the time yet to finish it up. Since this is a pretty high-requested feature (#1698, #1922) I thought I'd open a new issue to show what I've got so far.
The Problem
The idea behind a polymorphic many-to-many relationship is simple. Let's take a tags table as an example. You'll probably want tags to be attached to your posts table as well as your pages table, etc... Currently you'd have to implement 2 different belongsToMany relationships on your Tag model. While this is fine as long as you only have 2 or 3 relationships, things can quickly become cumbersome if you have to attach yours tags table to a lot of other models. You also don't have a single way to check for relationships, you'd always have to check each table separately.
The Solution
The solution is fairly simple. Add a new polymorphic many-to-many relationship where the polymorphic relationships are set on the pivot table, rather than the polymorphic model.
The database scheme would look as follows:
tags
- id
- title
tag_tagable (pivot)
- id
- tag_id
- tagable_id
- tagable_type
posts
- id
- title
pages
- id
- title
Possible records for the pivot table:
id | tag_id | tagable_id | tagable_type
-- | ------ | ---------- | ------------
1 | 1 | 1 | Post
2 | 1 | 1 | Page
3 | 2 | 1 | Post
4 | 3 | 2 | Page
5 | 2 | 2 | Post
Because we put the polymorphic relationship columns on the pivot table we get a very flexibly way to set relationships towards other models.
Implementing the relationships on models could look as follows:
class Tag extends Eloquent {
public function tagable()
{
return $this->morphToPivot();
}
}
class Post extends Eloquent {
public function tags()
{
return $this->morphBelongsToMany('Tag', 'tagable');
}
}
class Page extends Eloquent {
public function tags()
{
return $this->morphBelongsToMany('Tag', 'tagable');
}
}
The reason why I've created a new function here (
morphToPivot) is because the currentmorphTofunction assumes the polymorphic relationships are on the called model and not on the pivot table.
Now that we've set the relationships, we can begin using them. For instance if we'd want to grab all of the records attached to a single tag:
foreach ($tag->tagable as $tagable)
{
if ($tagable instanceof Post)
{
echo 'This is a Post model';
}
elseif ($tagable instanceof Page)
{
echo 'This is a Page model';
}
}
If we want to grab all of the tags for a single page or post:
foreach ($post->tags as $tag) {}
foreach ($page->tags as $tag) {}
You see? You get all the benefit of many-to-many relationships and polymorphic relationships.
This also solves the problem for checking relationships on each table separately. For example, if you'd want to check how many items are attached to a single tag you could do this:
$count = $tag->tagable()->count();
Current Progress
Now, I've already been working on the implementation for this and I've got it finished for about 50% so far. Everything for the morphBelongsToMany relationships works so far.
The only thing that still needs to be done is the morphToPivot function so it works the other way around as well.
You can find all of my code in this commit: https://github.com/driesvints/framework/commit/2f3af990860a08ac37ab5dab71207ff39944e8d6
I'd really like opinions on this and what you think would improve this.
Big thumbs up for this, super useful. I haven't gone in depth on the code behind it but the API you describe looks great to me.
Looks great to me +1
Awesome addition! I really hope this will be added to Laravel 4 Master a.s.a.p.!
Super useful!
Sounds solid. If I remember correctly Taylor wanted to implement this functionality anyways.
+999
Mike Funk mikefunk.com | [email protected]
On Wed, Aug 7, 2013 at 8:37 AM, BastianHofmann [email protected]:
Sounds solid. If I remember correctly Taylor wanted to implement this functionality anyways.
— Reply to this email directly or view it on GitHubhttps://github.com/laravel/framework/issues/2032#issuecomment-22247810 .
Great!!!
What is the expected timeline for this addition? Is it feasible to have this pushed into Master in say... a week? I really need this functionality. Adding it manually to /vendor/laravel won't work, since I heavily use "composer update".
Can't say when it's going to be added (or if). Taylor will need to take a look at it first when he finds some time. I don't think this will be added within a week.
You can probably just extend the Model and add the classes to an autoloaded directory. Or if Taylor doesn't like it, this can be created as a package, right?
Would be great if this becomes available as a package. In that way it's quite easy to convert as soon as Laravel natively supports this functionality.
@rhulshof What ever happens this would never go in to the 4.0.x branch, it would go in to 4.1 which is slated for November currently.
+1 Package or Native. I would be working on something similar to later this year.
I guess it's better to be package.
I'm looking at this. Just FYI.
For the people needing this soon you could create your own implementation adn just have your model extend the custom code instead of Eloquent/Model . Then change over if this becomes available. No need to edit vendor folders....
What makes you think I won't have it ready soon? :)
Taylor Otwell Sent with Airmail
On August 16, 2013 at 10:09:13 PM, Cody Covey ([email protected]) wrote:
For the people needing this soon you could create your own implementation adn just have your model extend the custom code instead of Eloquent/Model . Then change over if this becomes available. No need to edit vendor folders....
— Reply to this email directly or view it on GitHub.
Niicceeeee!
Does this also mean Eager Loading will work on Polymorphic relations, like described here: https://github.com/laravel/laravel/issues/1681#issuecomment-17104738 ?
Nice!
+1
This would make my day...