我正在尝试获取Laravel 5.6中的2列(“已发布”,“过期”)的计数。
我希望我的结果计数仅显示项目是否已发布,而不是过期或没有过期日期。
我的代码:
$today = date('Y-m-d');
$numrecords = deals::where('published',1)
->orwhereDate('expires', '=', '')
->orwhereDate('expires', '>=', $today)
->count();谢谢
Nabi
发布于 2019-02-21 04:16:52
$numrecords = deals::where('published', 1)
->where(function($q){
return $q->whereNull('expires')
->orwhereDate('expires', '>=', Carbon::today());
})
->count();以上内容将为您提供:
SELECT * FROM deals
WHERE
published = 1 AND
( expires IS NULL OR DATE(expires) >= today_date)发布于 2019-02-21 04:15:22
以下是您应该编写的查询:(今天是Laravel函数)
// 1. if the item is published
$item->where('published', true);
// 2. expire date is after today
$item->where('expiry_date', '>', today());
// 3. expire date is null
$item->whereNull('expiry_date');
//4. emit expired items
$item->where('expiry_date', '>', today());我认为你的一些检查条件是错误的。
此外,您的逻辑也非常混乱。我不确定你想写什么条件,但一定要写下来,在什么条件下,一个项目被考虑到计数,然后你决定使用where或orWhere。
https://stackoverflow.com/questions/54794426
复制相似问题