RoleController.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use App\Http\Controllers\Controller;
  6. use App\User;
  7. use App\Models\Role;
  8. use App\Models\Permission;
  9. use Validator;
  10. use DB;
  11. class RoleController extends Controller
  12. {
  13. public function index(){
  14. $data['title1'] = "Roles List";
  15. $data['title2'] = "Roles Add";
  16. $data['all_roles'] = Role::orderBy('display_name','ASC')->get();
  17. $data['all_permissions'] = Permission::orderBy('display_name','ASC')->get();
  18. return view('admin.role.index',$data);
  19. }
  20. public function store(Request $request){
  21. $this->validate($request,[
  22. 'name' => 'required|unique:roles,name',
  23. 'display_name' => 'required',
  24. 'permission' => 'required',
  25. ]);
  26. $role = new Role();
  27. $role->name = $request->name;
  28. $role->display_name = $request->display_name;
  29. $role->save();
  30. if (!empty($request->input('permission'))) {
  31. foreach ($request->input('permission') as $key => $value) {
  32. $role->attachPermission($value);
  33. }
  34. }
  35. return back()->with('msg','Data Insert successful !');
  36. }
  37. public function edit($id){
  38. $data['title'] = "Role Edit";
  39. $data['role'] = Role::find($id);
  40. $data['all_permissions'] = Permission::get();
  41. $data['rolePermissions'] = $rolePermissions = DB::table("permission_role")->where("permission_role.role_id",$id)->lists('permission_role.permission_id','permission_role.permission_id');
  42. return view('admin.role.edit',$data);
  43. }
  44. public function update(Request $request,$id){
  45. $this->validate($request,[
  46. 'name' => 'required',
  47. 'display_name' => 'required',
  48. 'permission' => 'required',
  49. ]);
  50. $role = Role::find($id);
  51. $role->name = $request->name;
  52. $role->display_name = $request->display_name;
  53. $role->update();
  54. if (!empty($request->input('permission'))) {
  55. DB::table("permission_role")->where("permission_role.role_id",$id)
  56. ->delete();
  57. foreach ($request->input('permission') as $key => $value) {
  58. $role->attachPermission($value);
  59. }
  60. }
  61. return redirect('admin/roles')->with('msg','Data Update successful !');
  62. }
  63. public function destroy($id){
  64. Role::whereId($id)->delete();
  65. return back()->with('msg','Data Delete successful !');
  66. }
  67. }