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.
167 lines
5.2 KiB
167 lines
5.2 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use App\Models\User; |
|
use App\Models\m_role; |
|
use App\Models\m_warehouse; |
|
use Illuminate\Http\Request; |
|
use Illuminate\Support\Collection; |
|
use Illuminate\Support\Facades\Auth; |
|
use App\Http\Requests\ValidasiCreateUser; |
|
|
|
class M_userController extends Controller |
|
{ |
|
/** |
|
* Display a listing of the resource. |
|
*/ |
|
public function index(Request $request) |
|
{ |
|
$perPage = $request->input('perPage', 5); |
|
|
|
$query = User::orderBy('created_at', 'desc'); |
|
|
|
// Add search logic similar to the Transfer index controller |
|
$search = $request->input('search') ?? ''; |
|
if ($search) { |
|
$query->where(function ($q) use ($search) { |
|
$q->where('fullname', 'like', "%$search%") |
|
->orWhere('email', 'like', "%$search%") |
|
->orWhere('no_hp', 'like', "%$search%") |
|
->orWhere('address', 'like', "%$search%") |
|
->orWhereHas('warehouse', function ($warehouseQuery) use ($search) { |
|
$warehouseQuery->where('name', 'like', "%$search%"); |
|
}); |
|
}); |
|
} |
|
|
|
if ($perPage == 'Semua') { |
|
$chunkSize = 100; |
|
$users = new Collection(); |
|
$currentPage = 1; |
|
|
|
$query->chunk($chunkSize, function ($userChunk) use ($users, &$currentPage) { |
|
foreach ($userChunk as $user) { |
|
$user->setAttribute('i', ($currentPage - 1) * $userChunk->perPage() + 1); |
|
$users->push($user); |
|
$currentPage++; |
|
} |
|
}); |
|
} else { |
|
$users = $query->paginate($perPage); |
|
} |
|
|
|
$data = [ |
|
'users' => $users, |
|
'i' => ($users->currentPage() - 1) * $users->perPage() + 1, |
|
'search' => $search, |
|
'active' => 'menu-user', |
|
]; |
|
|
|
return view('dashboard.Master_Data.User.index', $data); |
|
} |
|
|
|
/** |
|
* Show the form for creating a new resource. |
|
*/ |
|
public function create() |
|
{ |
|
$data = [ |
|
'warehouse' => m_warehouse::get(), |
|
'role' => m_role::get(), |
|
'active' => 'menu-user', |
|
]; |
|
return view('dashboard.Master_Data.User.create', $data); |
|
} |
|
|
|
/** |
|
* Store a newly created resource in storage. |
|
*/ |
|
public function store(ValidasiCreateUser $request) |
|
{ |
|
|
|
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); |
|
return redirect()->route('dashboard.user.index')->with('success', 'Data User berhasil ditambahkan'); |
|
} catch (\Throwable $th) { |
|
return redirect()->back()->with('error', 'Data User Gagal Ditambah.'); |
|
} |
|
} |
|
|
|
/** |
|
* Display the specified resource. |
|
*/ |
|
public function show($id) |
|
{ |
|
$data = [ |
|
'warehouse' => m_warehouse::get(), |
|
'role' => m_role::get(), |
|
'user' => User::find($id), |
|
'active' => 'menu-user', |
|
]; |
|
return view('dashboard.Master_Data.User.show', $data); |
|
} |
|
|
|
/** |
|
* Show the form for editing the specified resource. |
|
*/ |
|
public function edit($id) |
|
{ |
|
$data = [ |
|
'warehouse' => m_warehouse::get(), |
|
'role' => m_role::get(), |
|
'user' => User::find($id), |
|
'active' => 'menu-user', |
|
]; |
|
return view('dashboard.Master_Data.User.edit', $data); |
|
} |
|
|
|
/** |
|
* Update the specified resource in storage. |
|
*/ |
|
public function update(Request $request, $id) |
|
{ |
|
$request->validate([ |
|
'username' => 'required', |
|
'fullname' => 'required', |
|
'email' => 'required|email', |
|
'divisi' => 'required', |
|
'role_id' => 'required', |
|
'warehouse_id' => 'required', |
|
'password' => 'required', |
|
]); |
|
// dd($request->all()); |
|
try { |
|
$user = User::findOrFail($id); |
|
$userData = $request->all(); |
|
|
|
// Menambahkan nama pengguna yang melakukan pembaruan |
|
$userData['updated_by'] = Auth::user()->fullname; |
|
|
|
$user->update($userData); |
|
return redirect()->route('dashboard.user.index')->with('success', 'Data User berhasil diperbaharui'); |
|
} 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'); |
|
} |
|
} |
|
}
|
|
|