在laravel中的特定时间显示通知消息

时间:2015-11-18 17:37:24

标签: php laravel laravel-4 notifications laravel-5

我尝试在特定时间显示通知消息,如下例

    //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) //。

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

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

2 个答案:

答案 0 :(得分:1)

我想我找到了解决方案,所以我会向它展示帮助或改进它 我发现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/>');
            }
}

现在我可以获得分配给用户的所有Notification msgs,在我们的案例中每个60m再次显示 Msg1在60m后,60m Msg2后,Msg2显示Msg3 ....

答案 1 :(得分:1)

我认为您可以添加$counter来获取通知数量msg

这使其更具可读性

     $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>");
   }
相关问题