产品模块中添加和删除功能

Original 2019-03-30 10:12:23 307
abstract:public function DoAdd(){    // 获取提交出来的数据    $data = Request::param();    // 将产品名独立出来    $title = $data['title'];    // 利用产品名作为查询条件查询对应的数据 &nbs



public function DoAdd()
{
   // 获取提交出来的数据
   $data = Request::param();
   // 将产品名独立出来
   $title = $data['title'];
   // 利用产品名作为查询条件查询对应的数据
   $info = ProductModel::where('title', $title)->find();
   // 判断是否查询到相同的产品名称
   if ($info == true) {
       // 返回提示信息
       return ['res' => 0, 'msg' => '产品标题重复!'];
   }
   // 加入添加时间
   $data['time'] = time();
   // 添加发布管理员
   $data['username'] = Session::get('username');
   // 实例化模型
   $product = new ProductModel();
   // 进行添加并验证
   if ($product->save($data)) {
       // 返回提示信息
       return ['res' => 1, 'msg' => '发布成功!'];
   } else {
       // 返回提示信息
       return ['res' => 0, 'msg' => '发布失败!'];
   }
}


public function del()
{
   // 获取需要删除的产品id
   $proId = Request::param('id');
   $product = new ProductModel();
   if ($product->destroy($proId)) {
       return ['res'=>1,'msg'=>'删除成功!'];
   }

Correcting teacher:天蓬老师Correction time:2019-03-30 10:21:18
Teacher's summary:在方法中, 尽可能避免直接使用new, $product = new ProductModel();, 可以通过对象注入, 或者 Facade 方式进行调用

Release Notes

Popular Entries