douzhang2680 2016-07-15 10:21
浏览 30
已采纳

像一篇文章/评论只是一次,laravel

Information: I have 2 tables(users for the users of the website and new_themes for themes/posts(whatever) created by the users.

!!!There is no relationship between these 2 tables.(A user can create a new post if he is logged in)

Tables:

Users:
1   id 
2   name    
3   email   
4   password

New_themes:
1   id
2   title
3   text
4   upVotes
5   downVotes
6   TagName 

What I want to do:

I created the feature that user can like the post(but he can like it unlimited number of times) but I want them to be able to like just once in a life time a post.

What I can't figure out is the idea/structure/logic of how to make it. I am thinking of creating a table . A "pivot" one for tables users and new_themes(many to many relationship in Laravel)

1 id_user
2 id_theme

Where the both of them have a composite primary key so they won't be any duplicate values, so if the user wants to vote once again the same post he won't because there can't be duplicate values.

Here is the problem:

If this is the solution, I have doubt and no idea on how to populate the pivot table when a user likes a theme. If this isn't the solution any advice or idea will be very helpful for me to finish my last feature of my wep app.

Thank you very much.

  • 写回答

2条回答 默认 最新

  • dongpiansui8755 2016-07-15 10:26
    关注

    What I can't figure out is the idea/structure/logic of how to make it. I am thinking of creating a table . A "pivot" one for tables users and new_themes(many to many relationship in Laravel)

    That's the way to go. So it would look like this:

    +---------+----------+
    | user_id | theme_id |
    +---------+----------+
    | 1       |        1 |
    +---------+----------+
    | 12      |        1 |
    +---------+----------+
    | 14354   |        1 |
    +---------+----------+
    

    To 'like' a theme, just insert the user and theme id into the table. The rest is just taking care of the response from the database - giving the user feedback for his try to like something more than once etc.

    And of course you should not forget the ability to unlike - so you need to delete from the table again... if you want users to unlike.

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP