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

Add Morph Many to Many (across a join table) #1922

Closed
atrauzzi opened this issue Jul 21, 2013 · 2 comments
Closed

Add Morph Many to Many (across a join table) #1922

atrauzzi opened this issue Jul 21, 2013 · 2 comments

Comments

@atrauzzi
Copy link
Contributor

It looks like I'm not able to map a many-to-many relationship across a join table using Laravel polymorphic relations.

I can however simulate half the solution by shoehorning a where clause into my relation method:

(the polymorphic columns are stored on the join table, and the join table goes out on a normal relation to the other class, although it might be nice to have double polymorphic relations in the future too!)

public function roles() {
return $this
    ->belongsToMany(
        'MyNamespace\MyPackage\Model\Role',
        'table_memberships',
        'authorizable_id'
        )
        ->where('authorizable_type', '=', get_called_class())
    ;
}

...unfortunately this doesn't set the string when calling save(). I suspect this is just a matter of combining the current many-to-many relation with a sprinkle of the morph-one-or-many or morph-many classes.

@atrauzzi
Copy link
Contributor Author

Right now I can create morph-many-to-many records by manually instantiating a related model:

$vegetarianMembership = new MyNamespace\MyPackage\Model\Membership();
$vegetarian->memberships()->save($vegetarianMembership);
$user->memberships()->save($vegetarianMembership);

But again, it would obviously be nicer if I could more simply go:

$user->roles()->attach($vegetarian);

@atrauzzi
Copy link
Contributor Author

See the following (as well as the migration and other model classes) project to get a better idea of how this could work: https://github.com/atrauzzi/authoritaire/blob/master/src/Atrauzzi/Authoritaire/Model/Authorizable.php

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

No branches or pull requests

2 participants