在 laravel 的特定时间显示通知消息
Show notification message in certain time in laravel
我尝试在特定时间显示通知消息,如下例
//get all MaintenanceService that assigned to user
$ms= MaintenanceService::where('user_id', Auth::user()->id)
->where('created_at','<', Carbon::now())->get();
//looping over coun($ms)
for($x = 0 ; $x< count($ms);$x++) {
// this var carrying created_at date for $ms[$x] and add
// notification_period value for example 60 second
$firstNot[$x] = $ms[$x]->created_at->addMinutes($ms[$x]->notification_period);
if(Carbon::now() > $firstNot[$x]) {
echo('notification fired at ' .$firstNot[$x]. '<br/>');
}
}
以上代码可以很好地显示通知消息一次
但现在我需要获取 $firstNot
并添加到其中 $ms[$x]->notification_period
使用 addMinutes()
然后显示消息 现在我有 $secondNot
然后 $secondNot->addMinutes(60)
// 再一次 .
我需要做这个过程,例如十次
但实际上我不知道如何开始,任何帮助
我想我找到了解决方案,所以我会展示它以寻求帮助或改进它
我发现 if()
条件下的问题应该使用 while()
替换它
用于遍历 $firstNot
并在每个循环中增加 $firstNot->addMinutes($minValue)
变成如下代码
$ms= MaintenanceService::where('user_id', Auth::user()->id)
->where('created_at','<', Carbon::now())->get();
for($x = 0 ; $x< count($ms);$x++) {
$firstNot[$x] = $ms[$x]->created_at->addMinutes($ms[$x]->notification_period);
while(Carbon::now() > $firstNot[$x]) {
echo('first notification '. $firstNot[$x] .
// here get $firstNot and add fo it $ms[$x]->notification_period
' next notification will fire at'.$firstNot[$x]->addMinutes($ms[$x]->notification_period) .
' i will fire every ' . $ms[$x]->notification_period .'M'. '<br/>');
}
}
现在我可以获得分配给用户的所有通知消息,在我们的例子中,每 60m 再次显示它 Msg1 after 60m Msg2 after 60m form Msg2 show Msg3 ....
我想你可以添加$counter
来获取通知消息的数量
这是为了让它更易读
$counter = 0;
while(Carbon::now() > $firstNot[$x]) {
echo("<div>".'notification ' . ++$counter . ' at ' . $firstNot[$x] .
' next notification will fire at ' .$firstNot[$x]->addMinutes($ms[$x]->notification_period)
.' i will fire every ' . $ms[$x]->notification_period . 'M' .'<br/><br/>'."</div>");
}
我尝试在特定时间显示通知消息,如下例
//get all MaintenanceService that assigned to user
$ms= MaintenanceService::where('user_id', Auth::user()->id)
->where('created_at','<', Carbon::now())->get();
//looping over coun($ms)
for($x = 0 ; $x< count($ms);$x++) {
// this var carrying created_at date for $ms[$x] and add
// notification_period value for example 60 second
$firstNot[$x] = $ms[$x]->created_at->addMinutes($ms[$x]->notification_period);
if(Carbon::now() > $firstNot[$x]) {
echo('notification fired at ' .$firstNot[$x]. '<br/>');
}
}
以上代码可以很好地显示通知消息一次
但现在我需要获取 $firstNot
并添加到其中 $ms[$x]->notification_period
使用 addMinutes()
然后显示消息 现在我有 $secondNot
然后 $secondNot->addMinutes(60)
// 再一次 .
我需要做这个过程,例如十次
但实际上我不知道如何开始,任何帮助
我想我找到了解决方案,所以我会展示它以寻求帮助或改进它
我发现 if()
条件下的问题应该使用 while()
替换它
用于遍历 $firstNot
并在每个循环中增加 $firstNot->addMinutes($minValue)
变成如下代码
$ms= MaintenanceService::where('user_id', Auth::user()->id)
->where('created_at','<', Carbon::now())->get();
for($x = 0 ; $x< count($ms);$x++) {
$firstNot[$x] = $ms[$x]->created_at->addMinutes($ms[$x]->notification_period);
while(Carbon::now() > $firstNot[$x]) {
echo('first notification '. $firstNot[$x] .
// here get $firstNot and add fo it $ms[$x]->notification_period
' next notification will fire at'.$firstNot[$x]->addMinutes($ms[$x]->notification_period) .
' i will fire every ' . $ms[$x]->notification_period .'M'. '<br/>');
}
}
现在我可以获得分配给用户的所有通知消息,在我们的例子中,每 60m 再次显示它 Msg1 after 60m Msg2 after 60m form Msg2 show Msg3 ....
我想你可以添加$counter
来获取通知消息的数量
这是为了让它更易读
$counter = 0;
while(Carbon::now() > $firstNot[$x]) {
echo("<div>".'notification ' . ++$counter . ' at ' . $firstNot[$x] .
' next notification will fire at ' .$firstNot[$x]->addMinutes($ms[$x]->notification_period)
.' i will fire every ' . $ms[$x]->notification_period . 'M' .'<br/><br/>'."</div>");
}