CurdControllerTrait.php 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/15
  6. */
  7. namespace crmeb\traits;
  8. use crmeb\services\JsonService;
  9. use think\Request;
  10. trait CurdControllerTrait
  11. {
  12. /**保存数据库
  13. * @param $id
  14. * @param $field
  15. */
  16. public function change_field($id,$field)
  17. {
  18. if(!isset($this->bindModel)) return exception('方法不存在!');
  19. if(!class_exists($this->bindModel)) return JsonService::fail('操作Model不存在!');
  20. $model = new $this->bindModel;
  21. $pk = $model->getPk();
  22. if(strtolower($pk) == strtolower($field)) return JsonService::fail('主键不允许修改!');
  23. $data = $model->where($pk,$id)->find();
  24. if(!$data) JsonService::fail('记录不存在!');
  25. $value = app('request')->post($field);
  26. if($value === null) return JsonService::fail('请提交需要编辑的数据!');
  27. $data->$field = $value;
  28. return false !== $data->save() ? JsonService::successful('编辑成功!') : JsonService::fail('编辑失败!');
  29. }
  30. }