pintu2/app/Http/Controllers/UserController.php
2024-02-27 22:53:57 +07:00

106 lines
3.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\UserRole;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class UserController extends Controller
{
public function index(){
$user = User::join('user_roles','user_roles_id','=','user_roles.id')
->select('users.*', 'user_roles.nama as nama_roles')
->orderBy('users.id', 'desc')
->get();
$role = UserRole::get();
return view('user.index',['title'=> 'Manajemen User | Admin Dashboard','user'=> $user,'role' => $role]);
}
public function store(Request $request){
$request->validate([
'nip'=>'required|numeric',
'nama_depan'=>'required',
'nama_belakang'=>'required',
'nama_gelar'=>'required',
'email'=>'required|email',
'ettd'=>'required',
],[
'nip.required'=>'NIP Wajib Diisi',
'nip.numeric'=>'NIP/NRP Wajib Berisi Angka saja',
'nama_depan.required'=>'Nama Depan Wajib Diisi',
'nama_belakang.required'=>'Nama Belakang Wajib Diisi',
'nama_gelar.required'=>'Nama Dengan Gelar Lengkap Wajib Diisi',
'email.required'=>'Email Wajib Diisi',
'ettd.required'=>'E-TTD Wajib Diisi',
]);
$user = new User;
$user->nip = $request->nip;
$user->nama_depan = $request->nama_depan;
$user->nama_belakang = $request->nama_belakang;
$user->nama_gelar = $request->nama_gelar;
$user->email = $request->email;
$user->ettd = $request->ettd;
$user->password = Str::random(12);
$user->user_roles_id = $request->user_roles_id;
if($user->save()){
return redirect()->route('users.index')->with('success','Data Telah Berhasil Disimpan');
}
return back()->withInput();
}
public function update(Request $request){
$request->validate([
'nip'=>'required|numeric',
'nama_depan'=>'required',
'nama_belakang'=>'required',
'nama_gelar'=>'required',
'email'=>'required|email',
'ettd'=>'required',
],[
'nip.required'=>'NIP Wajib Diisi',
'nip.numeric'=>'NIP/NRP Wajib Berisi Angka saja',
'nama_depan.required'=>'Nama Depan Wajib Diisi',
'nama_belakang.required'=>'Nama Belakang Wajib Diisi',
'nama_gelar.required'=>'Nama Dengan Gelar Lengkap Wajib Diisi',
'email.required'=>'Email Wajib Diisi',
'ettd.required'=>'E-TTD Wajib Diisi',
]);
$user = User::find($request->id);
$user->nip = $request->nip;
$user->nama_depan = $request->nama_depan;
$user->nama_belakang = $request->nama_belakang;
$user->nama_gelar = $request->nama_gelar;
$user->email = $request->email;
$user->ettd = $request->ettd;
$user->password = Str::random(12);
$user->user_roles_id = $request->user_roles_id;
if($user->save()){
return redirect()->route('users.index')->with('success','Data Telah Berhasil Diubah');
}
return back()->withInput();
}
public function destroy(Request $request){
$user = User::findOrFail($request->id);
if($user->delete()){
return redirect()->route('users.index')->with('success','Data Telah Dihapus');
}
return redirect()->route('users.index')->with('error','Data Gagal Dihapus');
}
public function userroles_index(){
$data = UserRole::get();
return view('user.user_roles_index',['title'=> 'Admin Dashboard','data'=> $data]);
}
}