TIFNJK_E41222887/app/Http/Controllers/DashboardController.php

54 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Department;
use App\Models\Employee;
use App\Models\Position;
use Illuminate\Http\Request;
use Inertia\Inertia;
class DashboardController extends Controller
{
public function index()
{
// Ringkasan Data
$stats = [
'total_employees' => Employee::count(),
'total_departments' => Department::count(),
'total_positions' => Position::count(),
];
// Data Grafik Gender (Pie Chart)
$genderData = Employee::selectRaw('gender, count(*) as total')
->groupBy('gender')
->get()
->map(fn($item) => [
'name' => $item->gender == 'L' ? 'Laki-laki' : 'Perempuan',
'value' => $item->total,
'fill' => $item->gender == 'L' ? '#3b82f6' : '#ec4899',
]);
// Data Grafik Departemen (Bar Chart)
$deptData = Department::withCount('employees')
->having('employees_count', '>', 0)
->get()
->map(fn($item) => [
'name' => $item->name,
'employees' => $item->employees_count,
]);
// Karyawan Terbaru (Table)
$latestEmployees = Employee::with(['department', 'position', 'user'])
->latest('join_date')
->take(5)
->get();
return Inertia::render('dashboard', [
'stats' => $stats,
'genderData' => $genderData,
'deptData' => $deptData,
'latestEmployees' => $latestEmployees,
]);
}
}