通过foreach更新具有不同值的多行 - Laravel Eloquent

时间:2016-10-05 11:29:20

标签: laravel laravel-5 eloquent

我正在尝试检索多行,使用foreach循环,并使用特定值更新每个行(基于切换)

我收到Method save does not exist错误。不确定使用Eloquent的语法应该是什么。

$appointmentreminders = Appointmentreminder::where('appointment_id', '=', $id)->get();

foreach ( $appointmentreminders as $appointmentreminder) 
{
           switch ($appointmentreminder->timeunit) {
               case "minute":
                   $reminder = clone($appointment->appointment_start);
                   $reminder->subMinutes($appointmentreminder->time);
                   break;

               case "hour":
                   $reminder = clone($appointment->appointment_start);
                   $reminder->subHours($appointmentreminder->time);
                   break;

               case "day":
                   $reminder = clone($appointment->appointment_start);
                   $reminder->subDays($appointmentreminder->time);
                   break;

               case "week":
                   $reminder = clone($appointment->appointment_start);
                   $reminder->subWeeks($appointmentreminder->time);
                   break;

               case "month":
                   $reminder = clone($appointment->appointment_start);
                   $reminder->subMonths($appointmentreminder->time);
                   break;
             }

             $appointmentreminders->reminder = $reminder;
             $appointmentreminders->save();
}

1 个答案:

答案 0 :(得分:0)

这样做

$appointmentreminders = Appointmentreminder::where('appointment_id', '=', $id)->get();

foreach ( $appointmentreminders as $appointmentreminder) 
{
  $appointmentreminder = Appointmentreminder::find($appointmentreminder->id);
  switch ($appointmentreminder->timeunit) {
     case "minute":
         $reminder = clone($appointment->appointment_start);
         $reminder->subMinutes($appointmentreminder->time);
         break;

     case "hour":
         $reminder = clone($appointment->appointment_start);
         $reminder->subHours($appointmentreminder->time);
         break;

     case "day":
         $reminder = clone($appointment->appointment_start);
         $reminder->subDays($appointmentreminder->time);
         break;

     case "week":
         $reminder = clone($appointment->appointment_start);
         $reminder->subWeeks($appointmentreminder->time);
         break;

     case "month":
         $reminder = clone($appointment->appointment_start);
         $reminder->subMonths($appointmentreminder->time);
         break;
   }

   $appointmentreminder->reminder = $reminder;
   $appointmentreminder->save();
}