[英]How to get the number of users who have groups in a certain period of time
有一個類別的用戶$分組的參與者,不,分組的開始時間$ start_time和結束的$ end_time,我需要推斷在特定date分組的用戶的數量范圍,我不知道如何寫最后一個條件,以及如何將dateRange鏈接到用戶,因為它們屬於groups參數。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use app\Group;
use app\Participants;
use Carbon\Carbon;
class ParticipantCounts extends Controller
{
public function ParticipantCountsWithGroups()
{
$participants = $Participants::has('groups')->get();
$dateRange = "DD.MM.YY-DD.MM.YY";
$splitTimeStamp = explode("-", $dateRange);
$start_time = $splitTimeStamp[0];
$end_time = $splitTimeStamp[1];
Carbon::parse($start_time)->format('Y-m-d');
Carbon::parse($end_time)->format('Y-m-d');
$participants->where(function($query)
{
$query->where('start_date')->whereBetween('end_date');
})->orWhere(function($query))
{
$query->where('end_time')->whereBetween('end_time');
}
}
}
以下應為您提供group_id以及該組中日期范圍內的特定計數。
Participants::join('groups', 'groups.id', '=', 'participants.group_id')
->select('participants.group_id', \DB::raw('count(*) as participant_count'))
->where('groups.start_time', '>=', $startDate)
->where('groups.end_time', '<=', $endDate)
->groupBy('participants.group_id')
->get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.