管理员的增删改查操作

Original 2018-11-30 15:05:01 509
abstract:
order('id', 'desc')->paginate(3); // 将数据赋值给模板 $this->view->users = $users; return $this->fetch(); } public function add() { // 判断提交方式,如果是POST提交,说明是要添加数据 if(request()->isPost()){ // 获取前台提交过来的数据 $data = Request::param(); // 获取添加的时间 $data['time'] = time(); $username = $data['username']; // 使用用户名来查询数据库是否有对应的数据 $res = UserModel::where('username', $username)->find(); // 判断数据是否存在 if ($res == true) { return ['res' => 0, 'msg' => '用户名已存在!']; } // 实例化模型 $user = new UserModel(); // 验证数据是否存入数据库 if ($user->save($data)) { return ['res' => 1, 'msg' => '添加成功!']; } else { return ['res' => 0, 'msg' => '添加失败!']; } } return $this->fetch(); } public function edit() { // 获取前台提交过来的数据 $data = Request::param(); // 通过用户id查询需要更新用户的所有数据 $user = UserModel::get($data['id']); // 判断提交方式,如果是POST提交,说明是修改数据 if(request()->isPost()){ $res = $user->save([ 'username' => $data['username'], 'time' => time(), ], ['id' => $data['id']]); if ($res) { return ['res' => 1, 'msg' => '修改成功!']; } } // 将数据赋值给模板 $this->assign('user',$user); return $this->fetch(); } public function del() { // 获取需要删除管理员的id $userId = Request::param('id'); if(UserModel::destroy($userId)) { return['res'=>1,'msg'=>'删除成功!']; } } }

以上为管理员的增删改查操作全部完成

Correcting teacher:天蓬老师Correction time:2018-11-30 16:06:53
Teacher's summary:其实, tp51 可以自动判断用户的请求类型的, 不必每次都使用isPost()

Release Notes

Popular Entries