[英]Laravel whereDoesntHave union whereHas
我正在尝试查询没有关系的模型并将其与有关系的模型联合,但出现 memory 耗尽的错误。
Post::whereDoesntHave('comments')
->union(Post::newQuery()->whereHas('comments')->getQuery())
->paginate();`
有人知道使用工会的更好方法吗?
使用以下代码更新您的帖子 Model。
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class); //Pass Foreign Key if other than id
}
}
使用此代码更新您的评论 Model。
class Comment extends Model
{
public function post()
{
return $this->belongsTo(Post::class);//Pass Foreign Key if other than id
}
public function user()
{
return $this->belongsTo(User::class);//Pass Foreign Key if other than id
}
public function scopeNoComment($query)
{
return $query->count() == 0;
}
}
$comments = $post->NoComment()->with('user')->paginate(10);
建立关系将很容易访问数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.