雄辩/ Laravel三向多对多关系 [英] Eloquent/Laravel Three-way Many-to-Many Relationship

查看:99
本文介绍了雄辩/ Laravel三向多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有三个数据库表:

我是全新的Laravel and Enloquent >


  1. 小部件

  2. 动作

  3. 用户

我已经建立了一个具有以下列的连接表:


  1. widget_id

  2. action_id

  3. user_id

  4. 时间戳

交点的点是保持交互日志 - 每次用户执行操作时,需要花费时间戳小部件。我看到如何使用雄辩的模型建立一个简单的多对多关系,并且这个工作正常,但我不知道如何建立一个更复杂的三向关系。



对于这个问题,即使我有简单的多对多关系(例如 widget to action 所以会有一个名为 action_widget 的表),如何在Enloquent中添加 action_widget 表的显式模型,以便跟踪关于每个关系的额外数据(例如,时间戳,评论字段等)。或者,我只是以一个完全错误的方式来解决这个问题?



作为ORM新手,我非常关心我能做什么!这种感觉是否消失? :p

解决方案

当您有类似于Categoty之类的双向关系时,枢轴工作正常。
但是如果你有3种方式的关系,我认为最好有一个名为WidgetUserAction的模型。



编辑:对于这些情况,新的hasManyThrough工作正常。


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

解决方案

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三向多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆