IndustryController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * @Author: your name
  4. * @Date: 2021-04-22 17:20:17
  5. * @LastEditTime: 2021-07-21 14:35:56
  6. * @LastEditors: Please set LastEditors
  7. * @Description: In User Settings Edit
  8. * @FilePath: \10dsm\app\Http\Controllers\Admins\IndustryController.php
  9. */
  10. namespace App\Http\Controllers\Admins;
  11. use App\Modes\Industry;
  12. use Illuminate\Http\Request;
  13. use App\Http\Controllers\Controller;
  14. class IndustryController extends Controller
  15. {
  16. public function index(Request $request)
  17. {
  18. $params=$request->all();
  19. if(!isset($params['page'])){
  20. $res = Industry::whereStatus(1)->select(['id', 'content', 'type'])->orderBy('id','asc')->get();;
  21. }else{
  22. $res = Industry::whereStatus(1)->select(['id', 'content', 'type'])->orderBy('id','asc')->paginate(perPage());;
  23. }
  24. return showJsonSucc('查询成功',$res);
  25. }
  26. //添加类型 2021/7/21
  27. public function add(Request $request)
  28. {
  29. $validator = \Validator::make($param = $request->all(), [
  30. 'content' => 'required',
  31. 'type' => 'required|numeric|between:1,2'
  32. ]);
  33. if ($validator->fails()) {
  34. return showJsonErr($validator->errors()->first());
  35. }
  36. $result = Industry::whereContent($param['content'])->first();
  37. if($result){
  38. return showJsonErr('分类已存在');
  39. }
  40. $item = new Industry();
  41. $item->content = $param['content'];
  42. $item->type = $param['type'];
  43. if($item->save()){
  44. return showJsonSucc('添加类型成功');
  45. }else{
  46. return showJsonError('添加类型失败');
  47. }
  48. }
  49. //删除/批量删除 2021/7/21
  50. public function delete(Request $request)
  51. {
  52. $validator = \Validator::make($param = $request->all(), [
  53. 'id' => 'required',
  54. ]);
  55. if ($validator->fails()) {
  56. return showJsonErr($validator->errors()->first());
  57. }
  58. $ids = explode(',',$param['id']);
  59. \DB::beginTransaction();
  60. try {
  61. foreach ($ids as $id) {
  62. //查询数据是否存在
  63. $Industry = Industry::whereId($id)->first();
  64. if (empty($Industry)) {
  65. return showJsonErr("抱歉,【id:{$id}】不存在");
  66. }
  67. //修改数据状态
  68. $Industry->status = 2;
  69. if(!$Industry->save()){
  70. return showJsonErr("抱歉,删除失败");
  71. }
  72. }
  73. \DB::commit();
  74. return showJsonSucc('删除成功');
  75. } catch (\Exception $e) {
  76. \DB::rollBack();
  77. return showJsonErr($e->getMessage());
  78. }
  79. }
  80. }