在拉拉维尔的特定时间显示通知消息


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)//再来一次.

例如,我需要做这个过程十次

但实际上我不知道我是如何开始的,任何帮助

我认为您可以添加$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>");
   }
我想

我找到了解决方案,所以我会展示它以寻求帮助或改进它
我发现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 后 60m Msg2 在 60m 表单 Msg2 显示 Msg3 ....