hasRepeated($data)) { return false; } $columns = $this->getTableColumns(); $model = $this->getModel(); foreach ($data as $k => $v) { if (!in_array($k, $columns)) { continue; } $model->setAttribute($k, $v); } return $model->save(); } public function update($primaryKey, $data): bool { if ($this->hasRepeated($data, $primaryKey)) { return false; } $columns = $this->getTableColumns(); $model = $this->query()->whereKey($primaryKey)->first(); foreach ($data as $k => $v) { if (!in_array($k, $columns)) { continue; } $model->setAttribute($k, $v); } return $model->save(); } public function hasRepeated($data, $id = 0): bool { $query = $this->query()->when($id, fn($query) => $query->where('id', '<>', $id)); if ((clone $query)->where('card_no', $data['card_no'])->exists()) { $this->setError('该身份证号码已存在'); return true; } return false; } }