https://gitee.com/lzh1995/thinkphp-document

php think build —module 模块名

php think make:controller 模块名/控制器名
php think make:controller 模块名/控制器名 —plain

php think make:model 模块名/模型名

编辑

新增

1、保存一条记录

  1. /**
  2. * 保存新建的资源
  3. *
  4. * @param \think\Request $request
  5. * @return \think\Response
  6. */
  7. public function save(Request $request)
  8. {
  9. $input = input();
  10. //建议用静态方法调用,可以传入数据,后面的第二个参数是过滤非数据表中的字段,不然报错
  11. \app\admin\model\Goods::create($input,true);
  12. return $this->success('添加成功','index');
  13. }

查找

1、根据id查找对象

  1. /**
  2. * 显示编辑资源表单页.
  3. *
  4. * @param int $id
  5. * @return \think\Response
  6. */
  7. public function edit($id)
  8. {
  9. $model = \app\admin\model\Goods::find($id);
  10. return view('edit',['goods'=>$model]);
  11. }

更新

1、根据id更新

  1. /**
  2. * 保存更新的资源
  3. *
  4. * @param \think\Request $request
  5. * @param int $id
  6. * @return \think\Response
  7. */
  8. public function update(Request $request, $id)
  9. {
  10. //接收参数
  11. $params = input();
  12. //验证规则
  13. $rule = [
  14. 'goods_name|商品名称' => 'require|max:100',
  15. 'goods_price|商品价格' => 'require|float|egt:0',
  16. 'goods_number|商品数量' => 'require|integer|egt:0'
  17. ];
  18. //错误信息(可选)
  19. $msg = [
  20. 'goods_price.float' => '商品价格必须是整数或者小数'
  21. ];
  22. //2、控制器验证
  23. $res = $this->validate($params, $rule, $msg);
  24. if($res !== true){
  25. //失败 $res 就是字符串错误信息
  26. $this->error($res);
  27. }
  28. //处理数据(修改数据到数据表) 最后一个参数是允许的字段
  29. \app\admin\model\Goods::update($params, ['id' => $id], true);
  30. //返回(跳转页面)
  31. $this->success('操作成功', 'admin/goods/index');
  32. }

删除

1、根据id删除

  1. /**
  2. * 删除指定资源
  3. *
  4. * @param int $id
  5. * @return \think\Response
  6. */
  7. public function delete($id)
  8. {
  9. //id参数检测 大于0的整数
  10. if(!preg_match('/^\d+$/', $id) || $id == 0){
  11. $this->error('参数错误');
  12. }
  13. //删除操作
  14. /*\app\admin\model\Goods::destroy($id);//软删除
  15. //\app\admin\model\Goods::destroy($id, true);//真删除
  16. //跳转到列表页
  17. $this->success('删除成功');*/
  18. $goods = \app\admin\model\Goods::find($id);
  19. if(empty($goods)){
  20. $this->error('数据已经不存在');
  21. }
  22. $goods->delete();//软删除
  23. //$goods->delete(true);//真删除
  24. //跳转到列表页
  25. $this->success('删除成功');
  26. }

对应的model

  1. namespace app\admin\model;
  2. use think\Model;
  3. use traits\model\SoftDelete;
  4. class Goods extends Model
  5. {
  6. //特殊表 没有前缀、前缀与配置文件不一致、模型名称和数据表名不对应
  7. //需要设置table属性(真实的完整的表名称)
  8. //protected $table = 'tpshop_goods';
  9. //设置使用软删除 trait
  10. use SoftDelete;
  11. //指定软删除需要更新的字段
  12. protected $deleteTime = 'delete_time';
  13. }

模型

时间字段

自动写入创建和更新的时间戳字段
文档在这:https://www.kancloud.cn/manual/thinkphp5/138668

验证

image.png有必要加上注释

1、独立验证

  1. /**
  2. * 保存新建的资源
  3. *
  4. * @param \think\Request $request
  5. * @return \think\Response
  6. */
  7. public function save(Request $request)
  8. {
  9. //接收参数
  10. $params = input();
  11. //1.独立验证
  12. // 定义验证规则
  13. $rule = [
  14. 'goods_name|商品名称' => 'require',
  15. 'goods_price|商品价格' => 'require|float|egt:0',
  16. 'goods_number|商品数量' => 'require|integer|egt:0'
  17. ];
  18. //定义错误提示信息(可选)
  19. $msg = [
  20. 'goods_price.float' => '商品价格必须是整数或者小数'
  21. ];
  22. //实例化验证类Validate
  23. $validate = new \think\Validate($rule, $msg);
  24. //执行验证
  25. if(!$validate->check($params)){
  26. //验证失败
  27. $error_msg = $validate->getError();
  28. $this->error($error_msg);
  29. }
  30. //添加数据到数据表 第二个参数true表示过滤非数据表字段
  31. \app\admin\model\Goods::create($params, true);
  32. //页面跳转
  33. $this->success('操作成功', 'admin/goods/index');
  34. }

2.控制器验证 (建议这个)

  1. /**
  2. * 保存新建的资源
  3. *
  4. * @param \think\Request $request
  5. * @return \think\Response
  6. */
  7. public function save(Request $request)
  8. {
  9. //接收参数
  10. $params = input();
  11. //2.控制器验证
  12. // 定义验证规则
  13. $rule = [
  14. 'goods_name|商品名称' => 'require',
  15. 'goods_price|商品价格' => 'require|float|egt:0',
  16. 'goods_number|商品数量' => 'require|integer|egt:0'
  17. ];
  18. //定义错误提示信息(可选)
  19. $msg = [
  20. 'goods_price.float' => '商品价格必须是整数或者小数'
  21. ];
  22. //调用控制器的validate方法
  23. $validate = $this->validate($params, $rule, $msg);
  24. if($validate !== true){
  25. //验证失败, $validate 就是一个字符串错误信息
  26. $this->error($validate);
  27. }
  28. //添加数据到数据表 第二个参数true表示过滤非数据表字段
  29. \app\admin\model\Goods::create($params, true);
  30. //页面跳转
  31. $this->success('操作成功', 'admin/goods/index');
  32. }