我有一个Comments
使用软删除的模型:它one-to-many
与我的Post
模型有关系。
我的网站将有一个与之关联的本机移动应用程序,当我发送有关帖子的信息时,我需要向它发送评论计数,并且由于某种原因,它会返回带有软删除项目的计数。
我已经让 Post 数组正常工作并使用发送评论计数
protected $appends = array('score','commentcount', 'ups', 'downs');
和
public function getCommentcountAttribute()
{
return DB::table('comments')
->where('post_id',$this->id)
->where('deleted_at','=',NULL)
->count();
}
在我的帖子模型中。我也试过
public function getCommentcountAttribute()
{
return $this->comments()->count();
}
和
public function getCommentcountAttribute()
{
return $this->comments()->whereNull('deleted_at')->count();
// also: return $this->comments()->where('deleted_at',NULL)->count();
}
同样在定义关系时,我尝试添加->whereNUll('deleted_at')
到 the->hasMany('Comment')
和 the 中->belongsTo('Post')
,但没有成功。
我检查了数据库并运行了我期望 Fluent 和 Eloquent 生成的 SQL
SELECT * FROM `comments` WHERE post_id=31 and deleted_at=null
(31 是我用来测试的帖子)。没有任何工作。让我知道你们是否需要查看更多特定功能,因为我不想发布我的整个模型。