create($this->filterData($data))){ DB::commit(); return $this->getKey(); }else{ throw new \Exception("添加失效"); } }catch (\Exception $exception){ DB::rollBack(); throw new FailedException('添加失败!请刷新重试'); } return false; } public function updateBy($id, array $data) { $updated = $this->where($this->getKeyName(), $id)->update($this->filterData($data)); return $updated; } public function deleteBy($id) { $this->where($this->getKeyName(), $id)->update(['is_deleted' => 1,'deleted_at' => date("Y-m-d H:i:s")]); } protected function filterData(array $data): array { // 表单保存的数据集合 $fillable = array_unique(array_merge($this->getFillable(), property_exists($this, 'form') ? $this->form : [])); foreach ($data as $k => $val) { if (is_null($val) || (is_string($val) && ! $val)) { unset($data[$k]); } if (! empty($fillable) && ! in_array($k, $fillable)) { unset($data[$k]); } if (in_array($k, [$this->getUpdatedAtColumn(), $this->getCreatedAtColumn()])) { unset($data[$k]); } } return $data; } }