【发布时间】:2017-07-19 13:57:09
【问题描述】:
我在 laravel (5.2) 中的查询遇到了一个非常奇怪的问题 - 我有一个从某个外部源(API)创建的集合,我正在尝试运行一个“where”查询来提取具体记录。
最初,我试图提取在当月提交的所有条目(因此,在本月的第一天之后)
$entries is the starting collection (time entries on a project - see end of post)
$thisMonthStart = (new Carbon('first day of this month'))->toDateString();
//value of this is 2017-02-01, and the issue is not resolved if I remove toDateString()
$entriesThisMonth = $entries->where('spent-at', '>', $thisMonthStart);
//returns an empty collection, but should have 15 results
现在真正奇怪的部分是,我试图让 $entries 'spent-at' 等于每月的第一天 - 应该有一个条目。如果我不明确指定比较运算符,我会得到我的预期结果:
$entriesThisMonth = $entries->where('spent-at', $thisMonthStart);
//one $entries returned, see end of post
但是,如果我指定 = 运算符
$entriesThisMonth = $entries->where('spent-at', '=', $thisMonthStart);
//empty collection returned
所以我现在很困惑 - 大概是我的原始集合中有问题,但为什么指定与不指定运算符有什么区别?我会认为这两个查询会给出相同的结果?
(显然,在尝试进行 比较时,无法指定运算符并不是很有帮助,但我主要只是对这两种语法之间的实际区别感兴趣,以及为什么它们给出不同的结果?)
我在任何地方都找不到关于这两个版本的查询如何工作的任何信息,所以如果预计它们会给出不同的结果 - 我认为它们应该是相同的,但也许有更深入了解的人可以解释一下这是什么原因造成的?
感谢任何可以解开谜团的人!
$entries 集合的示例以防万一(仅一条记录): (注意肯定有当月的记录,我知道这个例子太老了)
Collection {#952 ▼
#items: array:367 [▼
175412141 => DayEntry {#958 ▼
#_root: "request"
#_convert: true
#_values: array:16 [ …16]
+"id": "175412141"
+"notes": ""
+"spent-at": "2013-10-03"
+"hours": "0.75"
+"user-id": "595841"
+"project-id": "4287629"
+"task-id": "2448666"
+"created-at": "2013-10-03T18:07:54Z"
+"updated-at": "2013-11-01T12:50:51Z"
+"adjustment-record": "false"
+"timer-started-at": ""
+"is-closed": "false"
+"is-billed": "true"
+"started-at": "10:45"
+"ended-at": "11:30"
+"invoice-id": "3633772"
}
这就是 where 查询返回的内容没有运算符:
Collection {#954 ▼
#items: array:1 [▼
568944822 => DayEntry {#1310 ▼
#_root: "request"
#_convert: true
#_values: array:15 [▶]
+"id": "568944822"
+"notes": "Tweaking formatting on job ads and re shuffling ad order"
+"spent-at": "2017-02-01"
+"hours": "0.25"
+"user-id": "595841"
+"project-id": "4287629"
+"task-id": "2448666"
+"created-at": "2017-02-01T14:45:00Z"
+"updated-at": "2017-02-01T14:45:00Z"
+"adjustment-record": "false"
+"timer-started-at": ""
+"is-closed": "false"
+"is-billed": "false"
+"started-at": "14:30"
+"ended-at": "14:45"
}
]
}
【问题讨论】:
标签: laravel where-clause laravel-query-builder