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.
109 lines
3.5 KiB
109 lines
3.5 KiB
<?php |
|
|
|
namespace App\Http\Controllers; |
|
|
|
use App\Models\m_role; |
|
use Illuminate\Http\Request; |
|
use Illuminate\Support\Collection; |
|
use Illuminate\Support\Facades\Auth; |
|
use App\Http\Requests\ValidasiCreateRole; |
|
use App\Http\Requests\ValidasiUpdateRole; |
|
|
|
class RoleController extends Controller |
|
{ |
|
/** |
|
* Display a listing of the resource. |
|
*/ |
|
public function index(Request $request) |
|
{ |
|
$perPage = $request->input('perPage', 5); |
|
|
|
$query = m_role::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('name', 'like', "%$search%") |
|
->orWhere('description', 'like', "%$search%"); |
|
}); |
|
} |
|
|
|
if ($perPage == 'Semua') { |
|
$chunkSize = 100; |
|
$roles = new Collection(); |
|
$currentPage = 1; |
|
|
|
$query->chunk($chunkSize, function ($roleChunk) use ($roles, &$currentPage) { |
|
foreach ($roleChunk as $role) { |
|
$role->setAttribute('i', ($currentPage - 1) * $roleChunk->perPage() + 1); |
|
$roles->push($role); |
|
$currentPage++; |
|
} |
|
}); |
|
} else { |
|
$roles = $query->paginate($perPage); |
|
} |
|
|
|
$data = [ |
|
'roles' => $roles, |
|
'i' => ($roles->currentPage() - 1) * $roles->perPage() + 1, |
|
'search' => $search, |
|
'active' => 'menu-role', |
|
]; |
|
|
|
return view('dashboard.Master_Data.Role.index', $data); |
|
} |
|
|
|
/** |
|
* Store a newly created resource in storage. |
|
*/ |
|
public function store(ValidasiCreateRole $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 |
|
m_role::create($validatedData); |
|
return redirect()->back()->with('success', 'Data role Berhasil Ditambah.'); |
|
} catch (\Throwable $th) { |
|
return redirect()->back()->with('error', 'Data role Gagal Ditambah.'); |
|
} |
|
} |
|
|
|
/** |
|
* Update the specified resource in storage. |
|
*/ |
|
public function update(ValidasiUpdateRole $request, $id) |
|
{ |
|
try { |
|
$currentUser = Auth::user(); |
|
$role = m_role::findOrFail($id); // Cari peran berdasarkan ID |
|
|
|
$validatedData = $request->except('_token'); |
|
$validatedData['updated_by'] = $currentUser->fullname; // Menggunakan nama pengguna sebagai updated_by |
|
|
|
$role->update($validatedData); // Update data peran |
|
|
|
return redirect()->back()->with('success', 'Data role Berhasil Diperbarui.'); |
|
} catch (\Throwable $th) { |
|
return redirect()->back()->with('error', 'Data role Gagal Diperbarui.'); |
|
} |
|
} |
|
|
|
/** |
|
* Remove the specified resource from storage. |
|
*/ |
|
public function destroy($id) |
|
{ |
|
// dd("oke"); |
|
try { |
|
$role = m_role::findOrFail($id); |
|
$role->delete(); |
|
return redirect()->back()->with('success', 'Data role berhasil dihapus'); |
|
} catch (\Throwable $th) { |
|
return redirect()->back()->with('error', 'Data role gagal dihapus'); |
|
} |
|
} |
|
}
|
|
|