Description
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?
Activity
jenssegers commentedon Nov 27, 2013
Are all your models extending the included model class?
rcwsr commentedon Nov 27, 2013
Yes, I've tried using the alias and
use Jenssegers\Mongodb\Model as Eloquent;
. My Club class:And my User class is set up in a similar manner.
The relationship is successfully recorded in the memberships pivot-table.
rcwsr commentedon Nov 27, 2013
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.
jenssegers commentedon Nov 27, 2013
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.
rcwsr commentedon Nov 27, 2013
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.
jenssegers commentedon Nov 27, 2013
Are you using the dev-master version or the latest tag?
rcwsr commentedon Nov 27, 2013
"jenssegers/mongodb": "*"
jenssegers commentedon Nov 27, 2013
And you are using the latest version? BelongsToMany support has only been added 7 days ago.
rcwsr commentedon Nov 27, 2013
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.
jenssegers commentedon Nov 27, 2013
Run
composer update
to be sure :)rcwsr commentedon Nov 27, 2013
Thanks, that's sorted it. One question, is there a way to name the field in the model collection that holds the referenced ids?
jenssegers commentedon Nov 27, 2013
I should look into that to be sure. Will let you know asap.
jenssegers commentedon Nov 29, 2013
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:
Updating readme for #75
jenssegers commentedon Nov 29, 2013
I just added some documentation on this in the readme.
4 remaining items