AdminController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Admin;
  6. use App\Models\Role;
  7. use Auth;
  8. use Mail;
  9. use Session;
  10. use DB;
  11. class AdminController extends Controller
  12. {
  13. /**
  14. * Create a new controller instance.
  15. *
  16. * @return void
  17. */
  18. public function __construct()
  19. {
  20. $this->middleware('admin');
  21. }
  22. /**
  23. * Show the application dashboard.
  24. *
  25. * @return \Illuminate\Http\Response
  26. */
  27. public function index()
  28. {
  29. $loggedin = Auth::guard('admin')->user();
  30. $admins = Admin::get();
  31. return view('admin.admins.list',compact('admins'));
  32. }
  33. public function create()
  34. {
  35. $loggedin = Auth::guard('admin')->user();
  36. $roles = Role::where('guard_name','admin')->orderBy('name','ASC')->get();
  37. return view('admin.admins.add',compact('roles'));
  38. }
  39. /**
  40. * Store a newly created resource in storage.
  41. *
  42. * @param \Illuminate\Http\Request $request
  43. * @return \Illuminate\Http\Response
  44. */
  45. public function store(Request $request)
  46. {
  47. $loggedin = Auth::guard('admin')->user();
  48. $this->validate($request,[
  49. 'name' => 'required',
  50. 'email' => 'required|email|max:255|unique:admins',
  51. 'password' => 'required|min:6|confirmed'
  52. ]);
  53. DB::beginTransaction();
  54. try {
  55. $admin = new Admin;
  56. $admin->name = $request->name;
  57. $admin->email = $request->email;
  58. $admin->status = 1;
  59. $admin->password = bcrypt($request->password);
  60. $admin->save();
  61. $admin->assignRole($request->role);
  62. DB::commit();
  63. return redirect()->action('Admin\AdminController@index')->with('success','Successfully Created!');
  64. } catch (\Exception $e) {
  65. DB::rollBack();
  66. return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
  67. }
  68. }
  69. /**
  70. * Display the specified resource.
  71. *
  72. * @param int $id
  73. * @return \Illuminate\Http\Response
  74. */
  75. public function show($id)
  76. {
  77. $loggedin = Auth::guard('admin')->user();
  78. $admin = Admin::find($id);
  79. return view('admin.admins.view',compact('admin'));
  80. }
  81. /**
  82. * Show the form for editing the specified resource.
  83. *
  84. * @param int $id
  85. * @return \Illuminate\Http\Response
  86. */
  87. public function edit($id)
  88. {
  89. $loggedin = Auth::guard('admin')->user();
  90. $admin = Admin::find($id);
  91. $roles = Role::where('guard_name','admin')->orderBy('name','ASC')->get();
  92. return view('admin.admins.edit',compact('admin','roles'));
  93. }
  94. /**
  95. * Update the specified resource in storage.
  96. *
  97. * @param \Illuminate\Http\Request $request
  98. * @param int $id
  99. * @return \Illuminate\Http\Response
  100. */
  101. public function update(Request $request, $id)
  102. {
  103. $loggedin = Auth::guard('admin')->user();
  104. if($id == 1 && $loggedin->id != 1){
  105. return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
  106. }
  107. $this->validate($request,[
  108. 'name' => 'required'
  109. ]);
  110. DB::beginTransaction();
  111. try {
  112. $admin = Admin::find($id);
  113. $admin->name = $request->name;
  114. if($admin->email != $request->email){
  115. $this->validate($request,[
  116. 'email' => 'required|email|max:255|unique:admins'
  117. ]);
  118. $admin->email = $request->email;
  119. }
  120. if($request->password){
  121. $this->validate($request,[
  122. 'password' => 'required|min:6|confirmed'
  123. ]);
  124. $admin->password = bcrypt($request->password);
  125. }
  126. $admin->syncRoles($request->role);
  127. $admin->update();
  128. DB::commit();
  129. return redirect()->action('Admin\AdminController@index')->with('success','Successfully Updated!');
  130. }catch (\Exception $e) {
  131. DB::rollBack();
  132. return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
  133. }
  134. }
  135. public function edit_profile()
  136. {
  137. $loggedin = Auth::guard('admin')->user();
  138. $admin = Admin::find($loggedin->id);
  139. return view('admin.admins.profile',compact('admin'));
  140. }
  141. public function update_profile(Request $request)
  142. {
  143. $loggedin = Auth::guard('admin')->user();
  144. $this->validate($request,[
  145. 'name' => 'required'
  146. ]);
  147. DB::beginTransaction();
  148. try {
  149. $admin = Admin::find($loggedin->id);
  150. $admin->name = $request->name;
  151. /*if($admin->email != $request->email){
  152. $this->validate($request,[
  153. 'email' => 'required|email|max:255|unique:admins'
  154. ]);
  155. $admin->email = $request->email;
  156. }*/
  157. if($request->password){
  158. $this->validate($request,[
  159. 'password' => 'required|min:6|confirmed'
  160. ]);
  161. $admin->password = bcrypt($request->password);
  162. }
  163. $admin->update();
  164. DB::commit();
  165. return redirect()->action('Admin\AdminController@edit_profile')->with('success','Successfully Updated!');
  166. }catch (\Exception $e) {
  167. DB::rollBack();
  168. return redirect()->action('Admin\AdminController@edit_profile')->with('error', "Something Wrong!");
  169. }
  170. }
  171. /**
  172. * Remove the specified resource from storage.
  173. *
  174. * @param int $id
  175. * @return \Illuminate\Http\Response
  176. */
  177. public function destroy($id)
  178. {
  179. $loggedin = Auth::guard('admin')->user();
  180. if($id == 1 && $loggedin->id != 1){
  181. return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
  182. }
  183. $admin = Admin::find($id);
  184. DB::beginTransaction();
  185. try {
  186. Admin::whereId($id)->delete();
  187. DB::commit();
  188. return redirect()->action('Admin\AdminController@index')->with('success','Successfully Deleted!');
  189. }catch (\Exception $e) {
  190. DB::rollBack();
  191. return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
  192. }
  193. }
  194. public function statusUpdate($id)
  195. {
  196. $loggedin = Auth::guard('admin')->user();
  197. if($id == 1 && $loggedin->id != 1){
  198. return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!" );
  199. }
  200. $admin = Admin::find($id);
  201. if($admin->status == 0){
  202. $admin->status = 1;
  203. $status = 'published';
  204. }else{
  205. $admin->status = 0;
  206. $status = 'unpublished';
  207. }
  208. $admin->update();
  209. }
  210. }