status = $data['status']; $orderExpress->save(); $this->syncTraces($data['traces'], $orderExpress); } } /** * 更新物流信息 * * @param array $traces * @param mixed $orderExpress * @return void */ protected function syncTraces($traces, $orderExpress) { // 查询现有轨迹记录 $orderExpressLog = ExpressLog::where('order_express_id', $orderExpress->id)->select(); $log_count = count($orderExpressLog); if ($log_count > 0) { // 移除已经存在的记录 array_splice($traces, 0, $log_count); } // 增加包裹记录 foreach ($traces as $k => $trace) { $orderExpressLog = new ExpressLog(); $orderExpressLog->user_id = $orderExpress['user_id']; $orderExpressLog->order_id = $orderExpress['order_id']; $orderExpressLog->order_express_id = $orderExpress['id']; $orderExpressLog->content = $trace['content']; $orderExpressLog->change_date = $trace['change_date']; $orderExpressLog->status = $trace['status']; $orderExpressLog->save(); } } }