whereNotNull('course_id')->where('end_time', '<=', $dateTime)->get(); foreach ($courseAppointmentTotals as $courseAppointmentTotal) { // 次数复原 $courseType = CourseType::whereHas('courses', function ($query) use ($courseAppointmentTotal) { $query->where('id', $courseAppointmentTotal->course_id); })->first(); if (empty($courseType)) continue; // 获取一年后的日期 $oneYearLaterTimestamp = strtotime($courseAppointmentTotal->end_time) + (365 * 24 * 60 * 60); // 延期 $courseAppointmentTotal->end_time = date('Y-m-d H:i:s', $oneYearLaterTimestamp); // 重置总次数 $courseAppointmentTotal->total = $courseType->year_total; $courseAppointmentTotal->save(); } return self::SUCCESS; } }