简体   繁体   中英

Eloquent/Laravel Three-way Many-to-Many Relationship

I'm brand new to Laravel and Eloquent (and I have minimal experience with ORM in general).

Let's say I have three database tables:

  1. Widgets
  2. Actions
  3. Users

I have modelled one junction table that has columns for:

  1. widget_id
  2. action_id
  3. user_id
  4. timestamp

The point of the junction is to keep a log of interactions--take a timestamp every time a user performs an action on a widget . I see how to model a simple many-to-many relationship using Eloquent and have this working fine, but I'm not sure how to model a more complicated three-way relationship.

For that matter, even if I had a simply many-to-many relationship (say widget to action so there would be a table called action_widget ), how can I add an explicit model for the action_widget table in Eloquent, for the purpose of keeping track of extra data about each relationship (eg a timestamp, a comments field, etc). Or, am I just going about this in a totally wrong way?

Being new to ORM, I feel very confined as to what I can do! Does this feeling go away? :p

pivot work fine when you have 2 way relation like Categoty<->Article. but if you have 3 way relation i think it's better to have a model called WidgetUserAction.

Edit : For these cases the new "hasManyThrough" works fine.

Laravel can create the intermediate model automatically. They're called "pivot tables".

I suggest you read this section in the documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM