code)) { $customer->code = self::generateCustomerCode($customer); // 使用saveQuietly避免递归调用 $customer->saveQuietly(); } }); } public static function generateCustomerCode($customer) { // 生成唯一16位code,CM+14位 $unique = strtoupper(substr(md5($customer->id . microtime(true) . mt_rand(1000, 9999)), 8, 6)); $code = 'CM' . date('Ymd') .$unique; return $code; } // 静默保存方法,不触发事件 public function saveQuietly(array $options = []) { return static::withoutEvents(function () use ($options) { return $this->save($options); }); } }