So the belongsToMany relationship is a many-to-many relationship so a pivot table is required
Example we have a users
table and a roles
table and a user_roles
pivot table.
The pivot table has two columns, user_id
, foo_id
... foo_id
referring to the id
in roles table.
So to do this we write the following in the user
eloquent model:
return $this->belongsToMany('Role', 'user_roles', 'user_id', 'foo_id');
Now this looks for an id
field in users
table and joins it with the user_id
field in the user_roles
table.
Issue is I want to specify a different field, other than id
to join on in the users
table. For example I have bar_id
in the users table that I want to use as the local key
to join with user_id
From laravel's documentation, it is not clear on how to do this. In other relationships like hasMany
and belongsTo
we can specify local key
and foriegn key
but not in here for some reason.
I want the local key
on the users table to be bar_id
instead of just id
.
How can I do this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…