No Description

RoleController.php 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use App\Role;
  6. use App\Permission;
  7. class RoleController extends Controller
  8. {
  9. /**
  10. * @return \Illuminate\View\View
  11. */
  12. public function create()
  13. {
  14. $permissions = Permission::select('id','name','description','parent_id')->get();
  15. return view('admin/rolecreate', ['permissions' => $permissions]);
  16. }
  17. /**
  18. * @param Request $request
  19. * @return \Illuminate\Http\RedirectResponse
  20. */
  21. public function store(Request $request)
  22. {
  23. $this->validate($request, [
  24. 'name' => 'unique:role|required',
  25. 'permissions' => 'required'
  26. ],[
  27. 'name.required' => '角色名称不能为空',
  28. 'name.unique' => '角色名称已经存在',
  29. 'permissions.required' => '该角色权限不能为空',
  30. ]);
  31. $childs= $request->input('permissions');
  32. foreach($childs as $a){
  33. $role = new Role();
  34. $role->name = $request->input('name');
  35. $role->child = $a;
  36. $role->create_time = time();
  37. $role->save();
  38. }
  39. return redirect('/admin/permission/index?type=2')->with('info', '添加角色成功');
  40. }
  41. /**
  42. * @param $name
  43. * @return \Illuminate\View\View
  44. */
  45. public function edit($name)
  46. {
  47. $data['role'] = $name;
  48. $data['role_permissions'] = Role::select('child')->where('name', '=', $name)->get();
  49. $permissions = Permission::select('id','name','description','parent_id')->get();
  50. return view('admin/roleedit', ['permissions' => $permissions, 'data' => $data]);
  51. }
  52. /**
  53. * @param Request $request
  54. * @return \Illuminate\Http\RedirectResponse
  55. */
  56. public function update(Request $request)
  57. {
  58. $this->validate($request, [
  59. 'permissions' => 'required'
  60. ],[
  61. 'permissions.required' => '该角色权限不能为空',
  62. ]);
  63. $childs= $request->input('permissions');
  64. Role::where('name', '=', $request->input('name'))->delete();
  65. foreach($childs as $a){
  66. $role = new Role();
  67. $role->name = $request->input('name');
  68. $role->child = $a;
  69. $role->create_time = time();
  70. $role->save();
  71. }
  72. return redirect('/admin/permission/index?type=2')->with('info', '修改角色成功');
  73. }
  74. }