【发布时间】:2019-09-08 11:58:51
【问题描述】:
我有一个statuses 表,我在其中存储多个模型(表)的状态。例如:
comments
id | status | text | ...
---------------------------
1 | 2 | great.. |
2 | 3 | thats.. |
posts
id | status | body | ...
---------------------------
1 | 4 | hey..|
2 | 5 | i w..|
statuses
id | type | title | ...
---------------------------
1 | comment | succes |
2 | comment | error |
3 | comment | published |
4 | post | deleted |
5 | post | pending |
这里我可以通过 belongsTo 关系访问相应的记录。
class Post extends Model {
public function status()
{
return $this->belongsTo(Status::class, ....);
}
}
但我想要的是急切加载 cmets 的状态选项,以便我可以下拉或类似的东西。我可以创建与 where 的自定义关系,但不会急于加载。
有什么建议吗?
【问题讨论】:
-
您可以在主模型中使用
with,并可以加载关系数据。喜欢Post::with('status')。参考this
标签: laravel