InnerDeviceNameController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Device;
  4. use App\Models\DeviceName;
  5. use App\Models\InnerDeviceNamesModel;
  6. use App\Models\Spec;
  7. use Illuminate\Http\Request;
  8. class InnerDeviceNameController extends BaseController
  9. {
  10. protected $model;
  11. protected $department;
  12. protected $model_name = '内部设备名称';
  13. protected $pre_uri = '/admin/InnerDeviceName/';
  14. protected $view_path = 'admin.inner-device-name.';
  15. protected $redirect_index = '/admin/InnerDeviceName/index';
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->model = new InnerDeviceNamesModel();
  20. }
  21. public function index()
  22. {
  23. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  24. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri'));
  25. }
  26. public function get(Request $request)
  27. {
  28. $items = $this->model->where('id', '>', 0)->orderBy('id','desc');
  29. if ($request->input('name'))
  30. {
  31. $items->where('name','like','%'.request('name').'%');
  32. }
  33. $items = $items->paginate(15);
  34. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  35. }
  36. public function create()
  37. {
  38. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  39. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  40. }
  41. public function store(Request $request)
  42. {
  43. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  44. $validator = $this->model->getValidator($request, 'store');
  45. if($validator->fails()) {
  46. return back()->withErrors($validator)->withInput();
  47. }
  48. $data = $request->input('data');
  49. $res = $this->model->create($data);
  50. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  51. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  52. }
  53. public function edit(Request $request)
  54. {
  55. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  56. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  57. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  58. }
  59. public function update(Request $request)
  60. {
  61. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  62. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  63. $validator = $this->model->getValidator($request, 'update');
  64. if($validator->fails()) {
  65. return back()->withErrors($validator)->withInput();
  66. }
  67. $data = $request->input('data');
  68. $res = $this->model->where('id', $request->input('id'))->update($data);
  69. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  70. return back()->with(['sg_success_info' => '编辑成功']);
  71. }
  72. public function delete(Request $request)
  73. {
  74. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  75. DeviceName::where('device_id', $item->id)->delete();
  76. Spec::where('device_id', $item->id)->delete();
  77. $res = $item->delete();
  78. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  79. return response()->json(['status' => 'success', 'info' => '操作成功']);
  80. }
  81. }