php - Laravel whereDoesntHave union whereHas
问题描述
我正在尝试查询没有关系的模型并将其与有关系的模型联合,但出现内存耗尽的错误。
Post::whereDoesntHave('comments')
->union(Post::newQuery()->whereHas('comments')->getQuery())
->paginate();`
有人知道使用工会的更好方法吗?
解决方案
使用以下代码更新您的帖子模型。
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class); //Pass Foreign Key if other than id
}
}
使用此代码更新您的评论模型。
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);
建立关系将很容易访问数据。
推荐阅读
- parameters - SciLab 如何求解具有不同时间参数的微分方程
- python - 在 PC(Windows 10)的哪个文件夹中 load_data() 将数据集保存在 Keras 中?
- windows - Delphi 10.2:32 位:表单不处理 SHELLHOOK 消息,为什么不呢?
- c# - 如何在 Azure 上托管和部署 ASP.Net core 2.0 webapi?
- ionic-framework - 离子加载控制器取消和返回
- c# - C# VSTO Excel NamedRange 更改事件:迭代范围
- java - 实现一个简单的复制工具
- c++ - setFramerateLimit() 函数在 sfml 中不起作用
- .net - 寻求有关 .NET 签名方案/策略的建议
- android - 如何将选定的id发送到android中的另一个布局