ProjectRoleController.php 4.2 KB

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