【发布时间】:2020-05-28 04:31:38
【问题描述】:
我想检索属于活动帖子的所有 cmets。
我的Posts 模型上有一个本地范围,看起来像这样。
public function scopePublic($query) {
return $query->whereHas('post', function ($q) {
$q->where('is_public', true);
});
}
效果很好,但只要我想将其转换为这样的全局范围,就会与 PHP message: PHP Fatal error: Allowed memory size of X bytes exhausted 中断:
static::addGlobalScope('is_public', function (Builder $builder) {
return $builder->whereHas('post', function ($q) {
$q->where('is_public', true);
});
});
我的最终目标是让所有评论查询仅显示公共 cmets,除非我明确要求不要这样做。
我已经通过了很多解决方案。我已尝试加入 cmets 上的帖子,并尝试添加子选择以失败。
$builder->addSelect(['is_public' => Post::select('is_private')
->whereColumn('id', 'comment.post_id')->limit(1)
]);
$builder->join('posts','posts.id','=','comments.post_id')
->where('comments.is_private', false);
【问题讨论】: