我有三种模式 - 博客,发布,评论。 博客有很多帖子 张贴有许多评论Laravel雄辩关系问题
当我写
return Blog::with('posts','posts.comments')->get();
它会给所有文章和评论的博客。 但是,我将如何获取由admin用户创建的博客,即注释表中的user_id。在哪写->where
条件。
return Blog::with('posts','posts.comments')->where('comments.user_id','=','23')->get();
给出错误。
SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "comments"
LINE 1: select * from "blogs" where "comments"."is_...
^ (SQL: select * from "blogs" where "comments"."user_id" = 23)
如何解决此问题。
请张贴错误 – user1012181
旁注:你只需要做'posts.comments'而不是两个,因为嵌套关系将加载它迭代的每个关系('posts'&'comments') – SamV
你的问题没有多大意义。您想要由某个用户创建的博客,但user_id位于评论表中。博客评论的作者为什么或者如何决定博客本身属于谁? – user3158900