首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自多列的Laravel Elequent计数

问来自多列的Laravel Elequent计数
EN

Stack Overflow用户
提问于 2019-02-21 04:05:20
回答 2查看 52关注 0票数 2

我正在尝试获取Laravel 5.6中的2列(“已发布”,“过期”)的计数。

我希望我的结果计数仅显示项目是否已发布,而不是过期或没有过期日期。

我的代码:

代码语言:javascript
复制
$today = date('Y-m-d'); 

 $numrecords = deals::where('published',1)
    ->orwhereDate('expires', '=', '')
    ->orwhereDate('expires', '>=', $today)
    ->count();

谢谢

Nabi

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-02-21 04:16:52

代码语言:javascript
复制
$numrecords = deals::where('published', 1)
    ->where(function($q){
        return $q->whereNull('expires')
            ->orwhereDate('expires', '>=', Carbon::today());
    })
    ->count();

以上内容将为您提供:

代码语言:javascript
复制
SELECT * FROM deals
WHERE
published = 1 AND 
( expires IS NULL OR DATE(expires) >= today_date)
票数 3
EN

Stack Overflow用户

发布于 2019-02-21 04:15:22

以下是您应该编写的查询:(今天是Laravel函数)

代码语言:javascript
复制
// 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。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54794426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档