简体   繁体   English

Laravel 4上两个以上的表或模型之间的关系

[英]Relation between more than two tables or models on Laravel 4

I have this method: 我有这种方法:

public function tags()
{
    return $this->belongsToMany("Tag","question_tags");
}

this get the Tag's details through the question_tags table and connects Tag with Questions like: 这样可以通过question_tags表获取标签的详细信息,并将标签问题相关联,例如:

Tags <- question_tags <- questions 标签<-question_tags <-问题

Now I need to get the tag's details but using this relations: 现在,我需要获取标签的详细信息,但要使用以下关系:

Tag <- question_revision_tags <- question_revisions <- questions 标签<-question_revision_tags <-question_revisions <-问题

but I can't make it work with a belongsToMany() or hasManyThrough() because there are more than 2 models implicated. 但由于涉及到两个以上的模型,因此我无法使其与belongsToMany()或hasManyThrough()一起使用。

you think, it would be better if I insert a column on question_revision_tags called question_id? 您认为,如果在question_revision_tags上插入一列称为question_id会更好? or is there a way to make this relation? 还是有办法建立这种关系?

In this case you can make models Tag, QuestionRevision and Question. 在这种情况下,您可以使模型成为Tag,QuestionRevision和Question。

I understand that right now you have "Question has many-to-many Tag-s" and it works fine. 我知道,现在您有了“问题具有多对多Tag-s”,并且效果很好。

Now you want to have 现在你想拥有

  • Question has one-to-many QuestionRevisions 问题具有一对多的QuestionRevisions
  • QuestionRevisionTags has many-to-many Tag-s QuestionRevisionTags具有多对多Tag

After building such relationships you can get all tags of the question and all questions of a tag also. 建立这样的关系后,您可以获取问题的所有标签以及标签的所有问题。 In this case there is no needfor 2 step many-to-many relationship. 在这种情况下,不需要两步多对多关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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