laravel的模型嵌套关联问题
现在做的类似朋友圈的接口查询
用户表 user 文章表 article 评论表 comment 点赞表 zan 结构如下
class User extand Model {
public function articles() {
return $this->hasMany(Article::class, 'user_id', 'id');
}
}
class Article extand Model {
public function comments() {
return $this->hasMany(Comment::class, 'article_id', 'id');
}
}
class Article extand Model {
public function zans() {
return $this->hasMany(Zan::class, 'article_id', 'id');
}
}
class Comment extand Model {
public function user() {
return $this->belongsTo(User::class, 'user_id', 'id');
}
}
class Zan extand Model {
public function user() {
return $this->belongsTo(User::class, 'user_id', 'id');
}
}
User::with('articles.comments.user')->where('id', 10)->get();
现在需求是根据用户 id 查询所有文章和文章下面的评论和点赞,评论、点赞里显示对应的用户信息。
User::with('articles.comments.user')->find(10);
`
上面只能查出一种,用morphTo
这种嵌套的没试出来,请问大家有没有高效的方法呢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样是可以实现,但是效率是不是很低呢、