-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Many to many, can't get related objects #75
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
Comments
Are all your models extending the included model class? |
Yes, I've tried using the alias and
And my User class is set up in a similar manner. The relationship is successfully recorded in the memberships pivot-table. |
I've just looked at my original post, I started writing something then forgot about it. Where I wrote: "When creating the relationship, nothing is stored Is this expected behavior?" I meant, nothing is stored in either model collection, but the relationship is stored in the pivot table. |
No it should not create a pivot table. It will add a 'membership_ids' to your user document and a 'user_ids' to your membership document. |
The pivot table is created whether or not I specify a pivot table name, or pass the relationship any data upon creation. Have I just accidentally created a feature?! A document from the pivot-collection in question, status is an extra field.
|
Are you using the dev-master version or the latest tag? |
|
And you are using the latest version? BelongsToMany support has only been added 7 days ago. |
Argh I think I might have an older version! Should I be using dev-master instead of *? I only set this laravel project up at the weekend, I assumed it would download the latest. |
Run |
Thanks, that's sorted it. One question, is there a way to name the field in the model collection that holds the referenced ids? |
I should look into that to be sure. Will let you know asap. |
You can, I just added a test for it in 34ea812 Just keep in mind that the second parameter for the belongsToMany methods does not actually do anything, since it defines the name of the pivot table. For example:
|
I just added some documentation on this in the readme. |
Thanks for this. |
I'm implementing a many to many relationship between two models, User and Club. The documentation says "The belongsToMany relation will not use a pivot "table", but will push id's to a related_ids attribute instead.".
However, I've set up
belongsToMany
on both models, and when attaching or syncing, a pivot table is created named, for example.user_club
. I can even specify the name of the pivot-table in my models, as well as pass extra attributes to store in the pivot-table when syncing or or attaching. When creating the relationship, nothing is stored Is this expected behavior?If it is, then I'm having trouble getting related items. For example, I'm trying to display all the users that belong to a club with
$users = $club->users();
. But in a var_dump, I can't see anything related to users. Am I doing something wrong?The text was updated successfully, but these errors were encountered: