Php 使用Laravel的复杂评论、标记、投票、收藏夹数据库关系

Php 使用Laravel的复杂评论、标记、投票、收藏夹数据库关系,php,mysql,laravel,polymorphism,has-and-belongs-to-many,Php,Mysql,Laravel,Polymorphism,Has And Belongs To Many,我试图通过在Laravel4.2中构建一个小而复杂的应用程序来学习HABTM和多态关系。它接收链接,让用户通过标签、评论、投票、收藏和关注等关键词和内容以及彼此进行交互。老实说,我已经在这方面工作了大约3天,感觉自己就像是在泥地里打转。这是故障- 型号: User, Link, Tag, Comment, Vote, Favorite, Follow 超级基本用例: USER -> ACTION -> TARGET User -> Submit -> Link,

我试图通过在Laravel4.2中构建一个小而复杂的应用程序来学习HABTM和多态关系。它接收链接,让用户通过标签、评论、投票、收藏和关注等关键词和内容以及彼此进行交互。老实说,我已经在这方面工作了大约3天,感觉自己就像是在泥地里打转。这是故障-

型号:

User, Link, Tag, Comment, Vote, Favorite, Follow
超级基本用例:

USER -> ACTION   -> TARGET
User -> Submit   -> Link, Tag, Comment
User -> Tag      -> User, Link
User -> Comment  -> User, Link
User -> Vote     -> Tag, Comment
User -> Favorite -> User, Link, Tag, Comment
User -> Follow   -> User, Tag
(假定)表格:

从这里。。我不确定我是否做对了。我一直在用不同的方式来定义一种关系,我只是想完全理解这种设计

我的模型文件看起来像什么?我可能会使用什么样的情况下belongtomany vs morphToMany?这是正确的方法还是我应该使用透视表进行探索?我希望能得到一些关于我需要学习什么才能正确实现这一点的建议

下面是我的User.php可能会是什么样子,但我不确定返回值应该是什么,因为我不太了解关系的需求

(假定)User.php


我觉得我就快成功了。我已经准备好了视图和路线,因为我已经重建了这个项目大约四次。我只需要努力解决所有这些数据库关系逻辑,这样我就可以让应用程序变得栩栩如生。非常感谢您的意见

以下是我的一些想法

  • 我真的很困惑是否使用belongtomany和morphotomany。但现在,随着我对拉威尔越来越熟悉,我越来越清楚地认识到,睡眠学家所能做的事情也可以由睡眠学家来完成。所以你只需要一个主人。它足够强大。但是,如果您想知道这两种关系之间的区别,下面是关于何时应该使用这两种关系的说明。对于belongtomany,您只需要获取一些实体,而不关心它们的状态。但是,如果需要区分这些实体之间的状态,例如投票(两种类型:向上投票和向下投票),则可以使用morphtomy
  • 对于假定的User.php,这些函数中的每一个都应该只返回一个sql查询实例。这些函数在控制器中用于获取结果。简单地说,模型中的函数只是不同的sql语句,包装得很好,可以在控制器中使用。通过这种方式,您可以将业务逻辑与sql选择语句分开
  • 在模型中调用函数时,确保调用->get()以确保sql语句的执行。本质上,模型中的每个函数只返回未执行的sql语句。如果要获取sql语句的结果,需要调用->get()

  • 希望这有帮助。如果您有更多问题,请随时提供帮助

    非常感谢您花时间阅读并回答。在第一点上,你不能只使用一个轴值而不是切换到morphtomy吗?我原来的系统有标记链接,但那是线性的。如果我想在其他模型上存储标记,我必须添加另一列。我正在使用一个自定义系统,在这里我使用指针,只是手工编写函数。我不明白拉威尔在幕后是如何处理事情的。
    users            [id, username]
    links            [id, user_id, url]
    tags             [id, user_id, text]
    commentables     [id, user_id, commentable, commentable_id, text] //how do i handle comment replies?
    taggables        [id, user_id, taggable, taggable_id, weight]
    votables         [id, user_id, voteable, voteable_od, vote]
    favorables       [id, user_id, favorable, favorable_id]
    followable       [id, user_id, followable, followable_id]
    
    usersFavored()    // Users which this User has Favored
    usersFollowed()   // Users which this User has Followed
    linksSubmitted()  // Links created by the User in the DB
    linksFavored()    // ...
    tagsOnSelf()      // Tags attached to this User by other Users
    tagsCreated()     // Tags which the User created in the DB
    tags()            // Tags which the User was the first to attach to a Link
    tagsVoted()       // Tags on which the User has Voted across the entire site
    tagsFavored()     // ...
    tagsFollowed()    // ...
    commentsOnSelf()  // ...
    comments()        // Comments across the site
    commentsFavored() // ...
    votesOnSelf()     // ...
    votes()           // Votes across the site
    favorites()       // Favorites across the site
    follows()         // Follows across the site