RoleController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Role;
  5. use App\Models\RoleTranslation;
  6. class RoleController extends Controller
  7. {
  8. /**
  9. * Display a listing of the resource.
  10. *
  11. * @return \Illuminate\Http\Response
  12. */
  13. public function index()
  14. {
  15. $roles = Role::paginate(10);
  16. return view('backend.staff.staff_roles.index', compact('roles'));
  17. }
  18. /**
  19. * Show the form for creating a new resource.
  20. *
  21. * @return \Illuminate\Http\Response
  22. */
  23. public function create()
  24. {
  25. return view('backend.staff.staff_roles.create');
  26. }
  27. /**
  28. * Store a newly created resource in storage.
  29. *
  30. * @param \Illuminate\Http\Request $request
  31. * @return \Illuminate\Http\Response
  32. */
  33. public function store(Request $request)
  34. {
  35. if($request->has('permissions')){
  36. $role = new Role;
  37. $role->name = $request->name;
  38. $role->permissions = json_encode($request->permissions);
  39. $role->save();
  40. $role_translation = RoleTranslation::firstOrNew(['lang' => env('DEFAULT_LANGUAGE'), 'role_id' => $role->id]);
  41. $role_translation->name = $request->name;
  42. $role_translation->save();
  43. flash(translate('Role has been inserted successfully'))->success();
  44. return redirect()->route('roles.index');
  45. }
  46. flash(translate('Something went wrong'))->error();
  47. return back();
  48. }
  49. /**
  50. * Display the specified resource.
  51. *
  52. * @param int $id
  53. * @return \Illuminate\Http\Response
  54. */
  55. public function show($id)
  56. {
  57. //
  58. }
  59. /**
  60. * Show the form for editing the specified resource.
  61. *
  62. * @param int $id
  63. * @return \Illuminate\Http\Response
  64. */
  65. public function edit(Request $request, $id)
  66. {
  67. $lang = $request->lang;
  68. $role = Role::findOrFail($id);
  69. return view('backend.staff.staff_roles.edit', compact('role','lang'));
  70. }
  71. /**
  72. * Update the specified resource in storage.
  73. *
  74. * @param \Illuminate\Http\Request $request
  75. * @param int $id
  76. * @return \Illuminate\Http\Response
  77. */
  78. public function update(Request $request, $id)
  79. {
  80. $role = Role::findOrFail($id);
  81. if($request->has('permissions')){
  82. if($request->lang == env("DEFAULT_LANGUAGE")){
  83. $role->name = $request->name;
  84. }
  85. $role->permissions = json_encode($request->permissions);
  86. $role->save();
  87. $role_translation = RoleTranslation::firstOrNew(['lang' => $request->lang, 'role_id' => $role->id]);
  88. $role_translation->name = $request->name;
  89. $role_translation->save();
  90. flash(translate('Role has been updated successfully'))->success();
  91. return redirect()->route('roles.index');
  92. }
  93. flash(translate('Something went wrong'))->error();
  94. return back();
  95. }
  96. /**
  97. * Remove the specified resource from storage.
  98. *
  99. * @param int $id
  100. * @return \Illuminate\Http\Response
  101. */
  102. public function destroy($id)
  103. {
  104. $role = Role::findOrFail($id);
  105. foreach ($role->role_translations as $key => $role_translation) {
  106. $role_translation->delete();
  107. }
  108. Role::destroy($id);
  109. flash(translate('Role has been deleted successfully'))->success();
  110. return redirect()->route('roles.index');
  111. }
  112. }