2017-10-17 61 views
1

我有,我想核对在Laravel星火结束审判日的数组:Laravel雄辩 - 其中created_at是在X天前

$trialIntervals = [10, 5, 3, 1, 0]; 

我取所有的球队,然后检查他们的trial_ends_at日起使用间隔diffInDays:

$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 

的目的是,在那里将只显示有一个是X一个created_at日期行几天前。

+0

工作,也许是因为在那里功能“ - >其中()”的第一个参数是字段名,所以碳::解析为在还没有感觉到 – LorenzoBerti

+1

Laravel第一参数有一个'whereDate()'函数,在这种情况下可能会帮助你很多。 –

回答

0

你有没有尝试过这样的:

$dayAgo = 6 // where here there is your calculation for now How many days 
$dayToCheck = Carbon::now()->subDays($dayAgo); 
$model->where("created_at", =, $dayToCheck); 
0

使用WhereRaw

->whereRaw('DATEDIFF(CURDATE(),STR_TO_DATE(created_at, '%Y-%m-%d'))'), $daysTillTrialEnds) 
0
->where(Carbon::parse('created_at')->diffInDays(Carbon::now()), $daysTillTrialEnds) 

到应该像

->where(Carbon::parse($item->created_at)->diffInDays(Carbon::now()), $daysTillTrialEnds) 
0

Laravel会自动打开 “created_at” 成碳对象,所以你没有解析它。

`$items->where('created_at')->diffInDays(Carbon::now(), $daysTillTrialEnds)' 

应该为你