我正在尝试从前一天的类别中选择最喜欢的图片。但是,我的查询返回空结果。我的照片通过多种形式与喜欢的人有联系。
这是我的查询:
$foodOfTheDay = Picture::withCount('likes')
->where('picture_type', 'food')
->whereHas('likes', function($query) {
$query->whereDate('created_at', Carbon::yesterday());
})
->orderBy('likes_count', 'desc')
->with('user')
->first();
这是我最喜欢的关系:
public function likes()
{
return $this->morphMany('App\Like', 'likeable');
}
谢谢您的帮助。
尝试一下:
$foodOfTheDay = Picture::withCount('likes')
->where('picture_type', 'food')
->whereHas('likes', function($query) {
$query->where('created_at', '>=', Carbon::yeserday()->startOfDay())
->where('created_at', '<=', Carbon::yesterday()->endOfDay());
})
->orderBy('likes_count', 'desc')
->with('user')
->first();