Siopas Inventory PETI for ISTW Website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.9 KiB

1 year ago
<?php
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\m_role;
use App\Models\m_warehouse;
use Illuminate\Http\Request;
12 months ago
use Illuminate\Support\Facades\Auth;
1 year ago
class M_userController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
// dd('oke');
12 months ago
$warehouse = m_warehouse::get();
$role = m_role::get();
1 year ago
$user = User::get();
11 months ago
return view('dashboard.Master_Data.User.index', compact('user', 'role', 'warehouse'));
1 year ago
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
11 months ago
$role = m_role::get();
$warehouse = m_warehouse::get();
return view('dashboard.Master_Data.User.create', compact('role', 'warehouse'));
1 year ago
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
12 months ago
$request->validate([
'username' => 'required',
'fullname' => 'required',
'email' => 'required|email',
'divisi' => 'required',
'role_id' => 'required',
'warehouse_id' => 'required',
'password' => 'required',
]);
try {
$currentUser = Auth::user();
$validatedData = $request->except('_token');
$validatedData['created_by'] = $currentUser->fullname; // Menggunakan nama pengguna sebagai created_by
$validatedData['updated_by'] = $currentUser->fullname; // Menggunakan nama pengguna sebagai updated_by
$validatedData['password'] = bcrypt($request->input('password')); // Enkripsi password
user::create($validatedData);
11 months ago
return redirect()->route('dashboard.user.index')->with('success', 'Data User berhasil ditambahkan');
12 months ago
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data User Gagal Ditambah.');
}
1 year ago
}
/**
* Display the specified resource.
*/
public function show($id)
{
$user = User::find($id);
$role = m_role::get();
$warehouse = m_warehouse::get();
11 months ago
return view('dashboard.Master_Data.User.show', compact('user', 'role', 'warehouse'));
1 year ago
}
/**
* Show the form for editing the specified resource.
*/
11 months ago
public function edit($id)
1 year ago
{
// dd('oke');
11 months ago
$user = User::find($id);
$role = m_role::get();
$warehouse = m_warehouse::get();
return view('dashboard.Master_Data.User.edit', compact('user', 'role', 'warehouse'));
1 year ago
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id)
{
$request->validate([
11 months ago
'username' => 'required',
1 year ago
'fullname' => 'required',
11 months ago
'email' => 'required|email',
1 year ago
'divisi' => 'required',
'role_id' => 'required',
'warehouse_id' => 'required',
11 months ago
'password' => 'required',
1 year ago
]);
// dd($request->all());
try {
$user = User::findOrFail($id);
12 months ago
$userData = $request->all();
// Menambahkan nama pengguna yang melakukan pembaruan
$userData['updated_by'] = Auth::user()->fullname;
$user->update($userData);
11 months ago
return redirect()->route('dashboard.user.index')->with('success', 'Data User berhasil diperbaharui');
1 year ago
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data User gagal diperbaharui');
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
// dd("oke");
try {
$user = User::findOrFail($id);
$user->delete();
return redirect()->back()->with('success', 'Data user berhasil dihapus');
} catch (\Throwable $th) {
return redirect()->back()->with('error', 'Data user gagal dihapus');
}
}
}