【发布时间】:2018-03-29 08:34:47
【问题描述】:
我想检查以 Laravel Spark 结束的试验的几天:
$trialIntervals = [10, 5, 3, 1, 0];
我获取所有团队,然后使用 diffInDays 检查他们的 trial_ends_at 日期是否为时间间隔之一:
$daysTillTrialEnds = Carbon::parse($team->trial_ends_at)->diffInDays(Carbon::now());
然后我检查这个值是否已经是数据库和间隔中的现有结果:
if ( in_array($daysTillTrialEnds, $trialIntervals) && !TrialEndingNotification::where('team_id', $team->id)->where('days_notified_at', $daysTillTrialEnds)->count() ) {
// not yet been added to db
}
我有一个名为“days_notified_at”的字段,我正在使用该字段检查我是否已经添加了该行并通知了客户。但是该字段不再使用,我宁愿引用 created_at 日期并删除该字段。
我的问题是我是如何做到这一点的:
->where(Carbon::parse('created_at')->diffInDays(Carbon::now()), $daysTillTrialEnds)
没有收到此错误:
DateTime::__construct(): Failed to parse time string (created_at) at position 0 (c): The timezone could not be found in the database
目的是 where 将仅显示 created_at 日期为 X 天前的行。
【问题讨论】:
-
可能是因为在 where 函数中“->where()”的第一个参数是字段名,所以 Carbon::parse 作为 where 的第一个参数没有意义
-
Laravel 有一个
whereDate()函数,在这种情况下可能会对你有很大帮助。
标签: php sql laravel laravel-5 eloquent