https://gitee.com/lzh1995/thinkphp-document
php think build —module 模块名
php think make:controller 模块名/控制器名
php think make:controller 模块名/控制器名 —plain
php think make:model 模块名/模型名
新增
1、保存一条记录
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
$input = input();
//建议用静态方法调用,可以传入数据,后面的第二个参数是过滤非数据表中的字段,不然报错
\app\admin\model\Goods::create($input,true);
return $this->success('添加成功','index');
}
查找
1、根据id查找对象
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
$model = \app\admin\model\Goods::find($id);
return view('edit',['goods'=>$model]);
}
更新
1、根据id更新
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//接收参数
$params = input();
//验证规则
$rule = [
'goods_name|商品名称' => 'require|max:100',
'goods_price|商品价格' => 'require|float|egt:0',
'goods_number|商品数量' => 'require|integer|egt:0'
];
//错误信息(可选)
$msg = [
'goods_price.float' => '商品价格必须是整数或者小数'
];
//2、控制器验证
$res = $this->validate($params, $rule, $msg);
if($res !== true){
//失败 $res 就是字符串错误信息
$this->error($res);
}
//处理数据(修改数据到数据表) 最后一个参数是允许的字段
\app\admin\model\Goods::update($params, ['id' => $id], true);
//返回(跳转页面)
$this->success('操作成功', 'admin/goods/index');
}
删除
1、根据id删除
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//id参数检测 大于0的整数
if(!preg_match('/^\d+$/', $id) || $id == 0){
$this->error('参数错误');
}
//删除操作
/*\app\admin\model\Goods::destroy($id);//软删除
//\app\admin\model\Goods::destroy($id, true);//真删除
//跳转到列表页
$this->success('删除成功');*/
$goods = \app\admin\model\Goods::find($id);
if(empty($goods)){
$this->error('数据已经不存在');
}
$goods->delete();//软删除
//$goods->delete(true);//真删除
//跳转到列表页
$this->success('删除成功');
}
对应的model
namespace app\admin\model;
use think\Model;
use traits\model\SoftDelete;
class Goods extends Model
{
//特殊表 没有前缀、前缀与配置文件不一致、模型名称和数据表名不对应
//需要设置table属性(真实的完整的表名称)
//protected $table = 'tpshop_goods';
//设置使用软删除 trait
use SoftDelete;
//指定软删除需要更新的字段
protected $deleteTime = 'delete_time';
}
模型
时间字段
自动写入创建和更新的时间戳字段
文档在这:https://www.kancloud.cn/manual/thinkphp5/138668
验证
1、独立验证
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//接收参数
$params = input();
//1.独立验证
// 定义验证规则
$rule = [
'goods_name|商品名称' => 'require',
'goods_price|商品价格' => 'require|float|egt:0',
'goods_number|商品数量' => 'require|integer|egt:0'
];
//定义错误提示信息(可选)
$msg = [
'goods_price.float' => '商品价格必须是整数或者小数'
];
//实例化验证类Validate
$validate = new \think\Validate($rule, $msg);
//执行验证
if(!$validate->check($params)){
//验证失败
$error_msg = $validate->getError();
$this->error($error_msg);
}
//添加数据到数据表 第二个参数true表示过滤非数据表字段
\app\admin\model\Goods::create($params, true);
//页面跳转
$this->success('操作成功', 'admin/goods/index');
}
2.控制器验证 (建议这个)
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//接收参数
$params = input();
//2.控制器验证
// 定义验证规则
$rule = [
'goods_name|商品名称' => 'require',
'goods_price|商品价格' => 'require|float|egt:0',
'goods_number|商品数量' => 'require|integer|egt:0'
];
//定义错误提示信息(可选)
$msg = [
'goods_price.float' => '商品价格必须是整数或者小数'
];
//调用控制器的validate方法
$validate = $this->validate($params, $rule, $msg);
if($validate !== true){
//验证失败, $validate 就是一个字符串错误信息
$this->error($validate);
}
//添加数据到数据表 第二个参数true表示过滤非数据表字段
\app\admin\model\Goods::create($params, true);
//页面跳转
$this->success('操作成功', 'admin/goods/index');
}