ProjectRoleController.php 4.1 KB

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