search

Home  >  Q&A  >  body text

laravel - Two models or tables produce multiple relationships. How to model

The requirements are like this,
I have user table and article table

user table structure

id
name
email
...

articleTable structure
id
title
content
...

The user table and article table will have the following relationship
1, Favorite
2, Like
3, Reply
4, Follow

Do I have to build 4 Models
article_user_collect
article_user_point
article_user_reply
article_user_attention

Each table only has user_id and article_id

The many-to-many relationship introduced in the laravel model is not suitable for this situation! ! !

过去多啦不再A梦过去多啦不再A梦2819 days ago446

reply all(3)I'll reply

  • 高洛峰

    高洛峰2017-05-16 16:53:06

    The many-to-many relationship introduced in the laravel model is not suitable for this situation! ! !

    Because this is not many-to-many.

    It is still recommended to create four tables such as comments and favorites

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-16 16:53:06

    Use a table to record the relationship between them
    article_user

    id (self-increment)
    articleId
    userId
    type 1. Collection 2. Like 3. Reply 4. Follow

    reply
    0
  • 黄舟

    黄舟2017-05-16 16:53:06

    Like table: likes 字段有user_id存点赞的用户的id,article_id存文章的id;
    评论表:comments 字段有 article_iduser_id, the same is true for collection table.

    reply
    0
  • Cancelreply