Eloquent 关系:用户有很多帖子有很多评论
Eloquent Relationships: user hasmany posts has many comments
我正在使用 laravel 并且我正在尝试使用 Users
创建我的数据库,这个用户将有很多 comments
和很多 posts
,问题是那post还需要有很多评论,都属于一个post。并且来自用户的评论将属于 post.
可能是我想的不对,想不通。
已收到帮助
一条评论可以 belongTo
同时 Post
和 Comment
。因此,解决方案可能如下所示:
人际关系
用户
hasMany
Post
hasMany
评论
Post
belongsTo
用户
hasMany
评论
评论
belongTo
用户
belongsTo
Post
型号
User
id
Post
id
user_id
Comment
id
user_id
post_id
我正在使用 laravel 并且我正在尝试使用 Users
创建我的数据库,这个用户将有很多 comments
和很多 posts
,问题是那post还需要有很多评论,都属于一个post。并且来自用户的评论将属于 post.
可能是我想的不对,想不通。
已收到帮助
一条评论可以 belongTo
同时 Post
和 Comment
。因此,解决方案可能如下所示:
人际关系
用户
hasMany
PosthasMany
评论
Post
belongsTo
用户hasMany
评论
评论
belongTo
用户belongsTo
Post
型号
User |
---|
id |
Post |
---|
id |
user_id |
Comment |
---|
id |
user_id |
post_id |