all(); if(!isset($params['page'])){ $res = Industry::whereStatus(1)->select(['id', 'content', 'type'])->orderBy('id','asc')->get();; }else{ $res = Industry::whereStatus(1)->select(['id', 'content', 'type'])->orderBy('id','asc')->paginate(perPage());; } return showJsonSucc('查询成功',$res); } //添加类型 2021/7/21 public function add(Request $request) { $validator = \Validator::make($param = $request->all(), [ 'content' => 'required', 'type' => 'required|numeric|between:1,2' ]); if ($validator->fails()) { return showJsonErr($validator->errors()->first()); } $result = Industry::whereContent($param['content'])->first(); if($result){ return showJsonErr('分类已存在'); } $item = new Industry(); $item->content = $param['content']; $item->type = $param['type']; if($item->save()){ return showJsonSucc('添加类型成功'); }else{ return showJsonError('添加类型失败'); } } //删除/批量删除 2021/7/21 public function delete(Request $request) { $validator = \Validator::make($param = $request->all(), [ 'id' => 'required', ]); if ($validator->fails()) { return showJsonErr($validator->errors()->first()); } $ids = explode(',',$param['id']); \DB::beginTransaction(); try { foreach ($ids as $id) { //查询数据是否存在 $Industry = Industry::whereId($id)->first(); if (empty($Industry)) { return showJsonErr("抱歉,【id:{$id}】不存在"); } //修改数据状态 $Industry->status = 2; if(!$Industry->save()){ return showJsonErr("抱歉,删除失败"); } } \DB::commit(); return showJsonSucc('删除成功'); } catch (\Exception $e) { \DB::rollBack(); return showJsonErr($e->getMessage()); } } }