我有一个 MySQL 的查询,但我需要将它转换成一个雄辩的模型 laravel 8。下面给出了查询,
$query = "SELECT group_id FROM `chat_histories` join chat_group on chat_group.id = chat_histories.group_id where chat_group.is_group = 1 and chat_histories.created_at BETWEEN '$startDate' and '$endDate' and chat_histories.deleted_at is null group by group_id";
$query = "select count(group_id) as total_chat_thread from ($query) total_chat";
DB::select($query);
到目前为止,我已经这样做了,
ChatHistory::leftJoin('chat_group', 'chat_group.id', '=', 'chat_histories.group_id')
->selectRaw('count(*) as totals')
->where('chat_group.is_group', 1)
->whereBetween('chat_histories.created_at', [$startDate, $endDate])
->groupBy('chat_histories.group_id')
->count('totals');
但这会返回一个列表,但我需要该列表的计数。这意味着它显示了 22 行,我需要这 22 行作为返回。
我的模型 ChatHistory 与 ChatGroup 的关系
public function chatGroup() {
return $this->belongsTo(ChatGroup::class, 'group_id', 'id');
}
我的模型 ChatGroup 与 ChatHistory 的关系
public function chatHistory() {
return $this->hasMany(ChatHistory::class,'group_id','id');
}
请帮助将其转换为雄辩的模型查询 在此先感谢。