email)->first(); if ($existingUser) { return back()->withErrors(['email' => 'Email sudah ada']); } if($user){ $user = new User(); $user->nama = $request->input('nama'); $user->email = $request->input('email'); $user->role_user = $request->input('role_user'); $user->no_telp = $request->input('no_telp'); $user->alamat = $request->input('alamat'); $user->password = Hash::make($request->input('password')); $user->save(); } return back()->with('success', 'User berhasil ditambahkan'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $user = User::findOrFail($id); $existingUser = User::where('email', $request->email)->where('id', '!=', $id)->first(); if ($existingUser) { return back()->withErrors(['email' => 'Email sudah ada']); } if ($user) { if ($request->filled('nama')) { $user->nama = $request->input('nama'); } if ($request->filled('email')) { $user->email = $request->input('email'); } if ($request->filled('role_user')) { $user->role_user = $request->input('role_user'); } if ($request->filled('no_telp')) { $user->no_telp = $request->input('no_telp'); } if ($request->filled('alamat')) { $user->alamat = $request->input('alamat'); } if ($request->filled('password')) { $user->password = Hash::make($request->input('password')); } $user->save(); return back()->with('success', 'User berhasil diupdate'); } return back()->withErrors(['user' => 'User tidak ditemukan']); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $user = User::findOrFail($id); $user->delete(); return back()->with('success', 'User telah dihapus.'); } }