dtzjvj3915 2017-07-28 08:13
浏览 27
已采纳

Laravel有很多很多关系

I have users, meetings and comments tables and I want users to be able to post comments on users profile and meetings.

I know how to make one to many relationships for users and comments tables, but I want all comments to be saved in one table and show comments on users profile and meetings.

This is my table structure:

users
id
name
comment_id

meetings
id
name

comments
id
user_id
comment
  • 写回答

2条回答 默认 最新

  • douzhi1937 2017-07-29 08:19
    关注

    TL;DR The solution is to use polymorphic relationships which Laravel makes really easy.

    IMHO I don't think that a many-to-many relationship will do the job. It would mean that:

    • a user can post multiple comments. Okay
    • a comment can belong to multiple user. Weird
    • a meeting can have multiple comments. Okay
    • a comment can belong to multiple meetings. Weird

    Plus you would likely need two comments tables to achieve that. For instance a comment_user table and a comment_meeting.

    The kind of relationship that would fit the most your situation is the polymorphic one. The name can be scary because it comes from ancient greek. But the concept behind the name is very simple. And Laravel makes polymorphic relationships very easy. You will certainly recognize your situation in the example given in the Laravel documentation ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 求给定范围的全体素数p的(p-2)的连乘积
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页