123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Models\Pelaporan;
- use App\Models\User;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Hash;
-
- class UsersController extends Controller
- {
- public function index(Request $request)
- {
- // $users = User::all();
- $user = $request->user();
- if ($user->role == 'admin') {
- // Jika pengguna adalah admin, hitung total berdasarkan tindakan_id
- $laporan = Pelaporan::with('tindakan','user')->get();
-
- } elseif ($user->role == 'user') {
- // Jika pengguna adalah pengguna biasa, hitung riwayat pelaporan milik pengguna tersebut
- $riwayat = Pelaporan::where('user_id', $user->id)->get();
- // Menghitung total riwayat pelaporan yang dimiliki oleh pengguna
- $laporan = $riwayat->with('tindakan','user')->count();
- }
- $users = User::query();
-
- if ($request->has('search')) {
- $users->where('name', 'LIKE', '%' . $request->search . '%');
- }
-
- $users = $users->get();
- return view('admin.user.index', compact('users', 'laporan'));
- }
-
- public function create()
- {
- $laporan = Pelaporan::with('tindakan','user')->get();
-
- return view('admin.user.create',compact('laporan'));
- }
-
- public function store(Request $request)
- {
- $request->validate([
- 'name' => 'required',
- 'email' => 'required|email|unique:users',
- 'password' => 'required|min:8|confirmed', // Menambahkan aturan confirmed untuk konfirmasi password
- 'role' => 'required',
- ], [
- 'name.required' => 'Nama wajib diisi',
- 'email.required' => 'Email wajib diisi',
- 'email.email' => 'Silahkan masukkan email yang valid',
- 'email.unique' => 'Email sudah pernah digunakan, silahkan pilih email yang lain',
- 'password.required' => 'Password wajib diisi',
- 'password.min' => 'Minimum password yang diizinkan 8 karakter',
- 'password.confirmed' => 'Konfirmasi password tidak sesuai dengan password',
- 'role.required' => 'Role wajib diisi',
- ]);
-
- $data = [
- 'name' =>$request->name,
- 'email' =>$request->email,
- 'role' =>$request->role,
- 'password' => Hash::make($request->password)
- ];
-
- User::create($data);
-
- return redirect()->route('user.index')->with('success', 'User berhasil ditambahkan.');
- }
-
-
- public function edit($id)
- {
- $laporan = Pelaporan::with('tindakan','user')->get();
- $users = User::find($id);
- return view('admin.user.update', compact('users', 'laporan'));
- }
-
- public function update(Request $request, $id)
- {
- $request->validate([
- 'name' => 'required',
- 'email' => 'required|email|unique:users,email,' . $id,
- 'role' => 'required',
- ], [
- 'name.required' => 'Nama wajib diisi',
- 'email.required' => 'Email wajib diisi',
- 'email.email' => 'Silahkan masukkan email yang valid',
- 'email.unique' => 'Email sudah pernah digunakan, silahkan pilih email yang lain',
- 'role.required' => 'Role wajib diisi',
- ]);
-
- $user = User::find($id);
-
- if (!$user) {
- return redirect()->route('user.index')->with('error', 'User tidak ditemukan.');
- }
-
- $user->name = $request->name;
- $user->email = $request->email;
- $user->role = $request->role;
-
- // Periksa apakah password diisi sebelum diupdate
- if ($request->filled('password')) {
- $request->validate([
- 'password' => 'required|min:8|confirmed',
- ], [
- 'password.required' => 'Password wajib diisi',
- 'password.min' => 'Minimum password yang diizinkan 8 karakter',
- 'password.confirmed' => 'Konfirmasi password tidak sesuai dengan password',
- ]);
-
- $user->password = Hash::make($request->password); // Gunakan Hash::make untuk mengenkripsi password baru
- }
-
- $user->save();
-
- return redirect()->route('user.index')->with('success', 'User berhasil diperbarui.');
- }
-
-
- public function destroy($id)
- {
- $user = User::find($id);
- $user->delete();
-
- return redirect()->route('user.index')->with('success', 'User berhasil dihapus.');
- }
- }
|