我有一個使用軟刪除的模型Comments
:它與我的Post
模型有one-to-many
關係。count()返回laravel中的軟刪除項目
我的網站將有一個原生的移動應用程序與它關聯,當我發送有關某個帖子的信息時,我需要向它發送一條評論的計數,並且由於某種原因,它將使用軟刪除的項目返回計數。
我已經得到了郵政陣列的同時發送在我的崗位模型中使用
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')
到兩個->hasMany('Comment')
和->belongsTo('Post')
沒有運氣的關係時。
我檢查了數據庫,並跑了我期待流利和雄辯是發電是
SELECT * FROM `comments` WHERE post_id=31 and deleted_at=null
(31是我使用測試後)的SQL。沒有任何工作。讓我知道,如果你們需要看到更多的具體功能,我寧願不發佈我的整個模型。
應該包括我已經做到了。除了當我使用' - > toArray()'和'count()'時,軟刪除工作無處不在 – MrJellyhands