Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 (e.g. 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

like image 830
Raolin Avatar asked May 31 '13 22:05

Raolin


1 Answers

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.

like image 200
Yahia Reyhani Avatar answered Sep 28 '22 05:09

Yahia Reyhani