【发布时间】:2018-02-13 17:04:39
【问题描述】:
我在检索多态关联“where related_object.column_name = x”时遇到问题。让我解释一下:
我的数据库中有许多“页面”,每个页面与不同类型的“内容”具有多态关联。例如:
页面
slug:我的页面,id:1
slug : my-other page, id: 2
music_track 表
music_track:id 10,page_id:1
视频表
视频:id 20,page_id:2
图片表
图片:id 30,page_id:1
目录
id: 555, contentable_id 10, contentable_type: App\music_track
id: 556, contentable_id 20, contentable_type: App\video
id: 557, contentable_id 30, contentable_type: App\image
我可以使用 Content::all( ) 返回所有内容,如何返回 page_id 为“1”的所有内容?
我似乎无法使用 Content::where('page_id', "=", "1"),因为 page_id 不明确,或者被假定为内容表的一部分。如何查询 RELATED 表?
【问题讨论】:
-
你的模型中定义了所有关系吗?
标签: php sql laravel laravel-5.1 polymorphic-associations