123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- namespace App\Http\Controllers\Admin;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use App\Models\Admin;
- use App\Models\Role;
- use Auth;
- use Mail;
- use Session;
- use DB;
- class AdminController extends Controller
- {
- /**
- * Create a new controller instance.
- *
- * @return void
- */
- public function __construct()
- {
- $this->middleware('admin');
- }
- /**
- * Show the application dashboard.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- $loggedin = Auth::guard('admin')->user();
- $admins = Admin::get();
- return view('admin.admins.list',compact('admins'));
- }
- public function create()
- {
- $loggedin = Auth::guard('admin')->user();
- $roles = Role::where('guard_name','admin')->orderBy('name','ASC')->get();
- return view('admin.admins.add',compact('roles'));
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function store(Request $request)
- {
- $loggedin = Auth::guard('admin')->user();
- $this->validate($request,[
- 'name' => 'required',
- 'email' => 'required|email|max:255|unique:admins',
- 'password' => 'required|min:6|confirmed'
- ]);
- DB::beginTransaction();
- try {
- $admin = new Admin;
- $admin->name = $request->name;
- $admin->email = $request->email;
- $admin->status = 1;
- $admin->password = bcrypt($request->password);
- $admin->save();
- $admin->assignRole($request->role);
- DB::commit();
-
- return redirect()->action('Admin\AdminController@index')->with('success','Successfully Created!');
- } catch (\Exception $e) {
- DB::rollBack();
- return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
- }
- }
- /**
- * Display the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function show($id)
- {
- $loggedin = Auth::guard('admin')->user();
- $admin = Admin::find($id);
- return view('admin.admins.view',compact('admin'));
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function edit($id)
- {
- $loggedin = Auth::guard('admin')->user();
- $admin = Admin::find($id);
- $roles = Role::where('guard_name','admin')->orderBy('name','ASC')->get();
- return view('admin.admins.edit',compact('admin','roles'));
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \Illuminate\Http\Request $request
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function update(Request $request, $id)
- {
- $loggedin = Auth::guard('admin')->user();
- if($id == 1 && $loggedin->id != 1){
- return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
- }
- $this->validate($request,[
- 'name' => 'required'
- ]);
- DB::beginTransaction();
- try {
- $admin = Admin::find($id);
- $admin->name = $request->name;
- if($admin->email != $request->email){
- $this->validate($request,[
- 'email' => 'required|email|max:255|unique:admins'
- ]);
- $admin->email = $request->email;
- }
- if($request->password){
- $this->validate($request,[
- 'password' => 'required|min:6|confirmed'
- ]);
- $admin->password = bcrypt($request->password);
- }
- $admin->syncRoles($request->role);
- $admin->update();
- DB::commit();
- return redirect()->action('Admin\AdminController@index')->with('success','Successfully Updated!');
- }catch (\Exception $e) {
- DB::rollBack();
- return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
- }
- }
- public function edit_profile()
- {
- $loggedin = Auth::guard('admin')->user();
- $admin = Admin::find($loggedin->id);
- return view('admin.admins.profile',compact('admin'));
- }
- public function update_profile(Request $request)
- {
- $loggedin = Auth::guard('admin')->user();
- $this->validate($request,[
- 'name' => 'required'
- ]);
- DB::beginTransaction();
- try {
- $admin = Admin::find($loggedin->id);
- $admin->name = $request->name;
- /*if($admin->email != $request->email){
- $this->validate($request,[
- 'email' => 'required|email|max:255|unique:admins'
- ]);
- $admin->email = $request->email;
- }*/
- if($request->password){
- $this->validate($request,[
- 'password' => 'required|min:6|confirmed'
- ]);
- $admin->password = bcrypt($request->password);
- }
- $admin->update();
- DB::commit();
- return redirect()->action('Admin\AdminController@edit_profile')->with('success','Successfully Updated!');
- }catch (\Exception $e) {
- DB::rollBack();
- return redirect()->action('Admin\AdminController@edit_profile')->with('error', "Something Wrong!");
- }
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param int $id
- * @return \Illuminate\Http\Response
- */
- public function destroy($id)
- {
- $loggedin = Auth::guard('admin')->user();
- if($id == 1 && $loggedin->id != 1){
- return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
- }
- $admin = Admin::find($id);
- DB::beginTransaction();
- try {
- Admin::whereId($id)->delete();
- DB::commit();
- return redirect()->action('Admin\AdminController@index')->with('success','Successfully Deleted!');
- }catch (\Exception $e) {
- DB::rollBack();
- return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!");
- }
- }
- public function statusUpdate($id)
- {
- $loggedin = Auth::guard('admin')->user();
- if($id == 1 && $loggedin->id != 1){
- return redirect()->action('Admin\AdminController@index')->with('error', "Something Wrong!" );
- }
- $admin = Admin::find($id);
- if($admin->status == 0){
- $admin->status = 1;
- $status = 'published';
- }else{
- $admin->status = 0;
- $status = 'unpublished';
- }
- $admin->update();
- }
- }
|