我编写了一个函数来根据日期过滤用户和代理预订的票证。这基本上是一个订票应用程序。在此用户和代理可以预订不同的票证,因此在管理仪表板中,我显示用户今天预订的票总数,反之亦然。因此,我使用Carbon来验证今天的日期。但是我得到了不同类型的输出!
控制器:
$ticketsTodayUsers = Booking::with('users')->where("createdAt", ">=", Carbon::today())->whereHas("users", function($subQuery){
$subQuery->where("usertype", "=", "normal");
})->get();
因此,在此我有两个表,预订表和用户表,通过它们,我正在验证今天使用carbon::today((创建的工单,用户类型为正常或代理。
但是当我今天(2018年9月19日(检查时,我得到了昨天的计数结果(2018年9月18日(。我没有得到今天的计数,它是零,而是计数来自昨天或很久以前,例如 14-se-2018。我不知道我哪里做错了!
主要条件是我需要每天分离用户和代理预订计数,因此通过使用 createdAt 从 db 归档和今天碳函数,我试图匹配两者。但我没有得到预期的答案!
尝试使用$tz参数 - 时区。
Carbon::today('Asia/Calcutta');
并尝试使用WhereData()
,WhereDay()
:
->whereDay('createdAt', '=', date('d'))
或
->whereDate('createdAt', Carbon::today())
当然有适当的时区
Booking::with('users')->whereDate('createdAt', Carbon::today('Asia/Calcutta')->toDateString())->whereHas("users", function($subQuery){
$subQuery->where("usertype", "=", "normal");
})->get();
试试这个:
$ticketsTodayUsers = Booking::with('users')
->whereDate("createdAt", ">=", Carbon::now()->toDateString())
->whereHas("users", function($subQuery){
$subQuery->where("usertype", "=", "normal");
})->get();
这对我有帮助
$ticketsTodayUsers = Booking::with('users')->whereBetween('created_at', [Carbon::now()->startOfDay(), Carbon::now()->addHour()])->get();