StaffController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Staff;
  5. use App\Models\Role;
  6. use App\Models\User;
  7. use Hash;
  8. class StaffController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index()
  16. {
  17. $staffs = Staff::paginate(10);
  18. return view('backend.staff.staffs.index', compact('staffs'));
  19. }
  20. /**
  21. * Show the form for creating a new resource.
  22. *
  23. * @return \Illuminate\Http\Response
  24. */
  25. public function create()
  26. {
  27. $roles = Role::all();
  28. return view('backend.staff.staffs.create', compact('roles'));
  29. }
  30. /**
  31. * Store a newly created resource in storage.
  32. *
  33. * @param \Illuminate\Http\Request $request
  34. * @return \Illuminate\Http\Response
  35. */
  36. public function store(Request $request)
  37. {
  38. if(User::where('email', $request->email)->first() == null){
  39. $user = new User;
  40. $user->name = $request->name;
  41. $user->email = $request->email;
  42. $user->phone = $request->mobile;
  43. $user->user_type = "staff";
  44. $user->password = Hash::make($request->password);
  45. if($user->save()){
  46. $staff = new Staff;
  47. $staff->user_id = $user->id;
  48. $staff->role_id = $request->role_id;
  49. if($staff->save()){
  50. flash(translate('Staff has been inserted successfully'))->success();
  51. return redirect()->route('staffs.index');
  52. }
  53. }
  54. }
  55. flash(translate('Email already used'))->error();
  56. return back();
  57. }
  58. /**
  59. * Display the specified resource.
  60. *
  61. * @param int $id
  62. * @return \Illuminate\Http\Response
  63. */
  64. public function show($id)
  65. {
  66. //
  67. }
  68. /**
  69. * Show the form for editing the specified resource.
  70. *
  71. * @param int $id
  72. * @return \Illuminate\Http\Response
  73. */
  74. public function edit($id)
  75. {
  76. $staff = Staff::findOrFail(decrypt($id));
  77. $roles = Role::all();
  78. return view('backend.staff.staffs.edit', compact('staff', 'roles'));
  79. }
  80. /**
  81. * Update the specified resource in storage.
  82. *
  83. * @param \Illuminate\Http\Request $request
  84. * @param int $id
  85. * @return \Illuminate\Http\Response
  86. */
  87. public function update(Request $request, $id)
  88. {
  89. $staff = Staff::findOrFail($id);
  90. $user = $staff->user;
  91. $user->name = $request->name;
  92. $user->email = $request->email;
  93. $user->phone = $request->mobile;
  94. if(strlen($request->password) > 0){
  95. $user->password = Hash::make($request->password);
  96. }
  97. if($user->save()){
  98. $staff->role_id = $request->role_id;
  99. if($staff->save()){
  100. flash(translate('Staff has been updated successfully'))->success();
  101. return redirect()->route('staffs.index');
  102. }
  103. }
  104. flash(translate('Something went wrong'))->error();
  105. return back();
  106. }
  107. /**
  108. * Remove the specified resource from storage.
  109. *
  110. * @param int $id
  111. * @return \Illuminate\Http\Response
  112. */
  113. public function destroy($id)
  114. {
  115. User::destroy(Staff::findOrFail($id)->user->id);
  116. if(Staff::destroy($id)){
  117. flash(translate('Staff has been deleted successfully'))->success();
  118. return redirect()->route('staffs.index');
  119. }
  120. flash(translate('Something went wrong'))->error();
  121. return back();
  122. }
  123. }