yusath

user controller

Jul 30th, 2021
692
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use Alert;
  7. use App\User;
  8. use Spatie\Permission\Models\Role;
  9. use Illuminate\Support\Facades\DB;
  10.  
  11. class UserController extends Controller
  12. {
  13.     /**
  14.      * Display a listing of the resource.
  15.      *
  16.      * @return \Illuminate\Http\Response
  17.      */
  18.     public function index()
  19.     {
  20.         //
  21.         $user=\App\User::all();
  22.         return view('admin.user',['user'=>$user]);
  23.     }
  24.  
  25.     /**
  26.      * Show the form for creating a new resource.
  27.      *
  28.      * @return \Illuminate\Http\Response
  29.      */
  30.     public function create()
  31.     {
  32.         //
  33.     }
  34.  
  35.     /**
  36.      * Store a newly created resource in storage.
  37.      *
  38.      * @param  \Illuminate\Http\Request  $request
  39.      * @return \Illuminate\Http\Response
  40.      */
  41.     public function store(Request $request)
  42.     {
  43.         $save=new \App\User;
  44.         $save->name=$request->get('username');
  45.         $save->email=$request->get('email');
  46.         $save->password=bcrypt('password');
  47.         if($request->get('roles')=="ADMIN"){
  48.             $save->assignRole('admin');
  49.         }else{
  50.             $save->assignRole('user');
  51.         }
  52.         $save->save();
  53.         Alert::success('success','Data saved successfully');
  54.         return redirect()->route('user.index');
  55.     }
  56.  
  57.     /**
  58.      * Display the specified resource.
  59.      *
  60.      * @param  int  $id
  61.      * @return \Illuminate\Http\Response
  62.      */
  63.     public function show($id)
  64.     {
  65.         $user=User::find($id);
  66.         return view('admin.user' ,compact('user'));
  67.     }
  68.  
  69.     /**
  70.      * Show the form for editing the specified resource.
  71.      *
  72.      * @param  int  $id
  73.      * @return \Illuminate\Http\Response
  74.      */
  75.     public function edit($id)
  76.     {
  77.         $user=User::find($id);
  78.         $roles=Role::pluck('name','name')->all();
  79.         return view('admin.edituser',compact('user','roles'.'userRole'));
  80.     }
  81.  
  82.     /**
  83.      * Update the specified resource in storage.
  84.      *
  85.      * @param  \Illuminate\Http\Request  $request
  86.      * @param  int  $id
  87.      * @return \Illuminate\Http\Response
  88.      */
  89.     public function update(Request $request, $id)
  90.     {
  91.         $user=User::find($id);
  92.         DB::table('model_has_roles')->where('model_id',$id)->delete();
  93.         $user->assignRole($request->input('role'));
  94.         Alert::success('update','Data Berhasil di update');
  95.         return redirect()->route('user.index');
  96.     }
  97.  
  98.     /**
  99.      * Remove the specified resource from storage.
  100.      *
  101.      * @param  int  $id
  102.      * @return \Illuminate\Http\Response
  103.      */
  104.     public function destroy($id)
  105.     {
  106.         //
  107.     }
  108. }
  109.  
RAW Paste Data