【发布时间】:2020-05-04 13:29:23
【问题描述】:
当您在 1 个表(模型)上有 2 个变形关系时,Laravel 中正确的命名约定是什么?
例子:
posts
id - integer
title - string
body - text
videos
id - integer
title - string
url - string
buyers
id - integer
name - string
sellers
id - integer
name - string
comments
id - integer
body - text
commentable1_id - integer // link to video/posts
commentable1_type - string // type link to video/posts
commentable2_id - integer // link to buyers/sellers
commentable2_type - string // type link to buyers/sellers
现在我使用名称 commentable1 和 commentable2 ,但这不是一个很好的 Laravel 方式。我怎样才能以正确的 Laravel 方式做到这一点?
【问题讨论】:
-
恕我直言,最好的约定是有意义的,您(尤其是其他人)可以轻松理解,尤其是在远离代码几个月后。有什么问题(假设在您的上下文中有意义)
content_comment用于视频/帖子,trader_comment用于买家/卖家 -
你有没有得到你要找的答案,如果有,如果你能把它作为答案发布就好了。
标签: laravel polymorphic-associations