【发布时间】:2016-10-14 06:53:45
【问题描述】:
我正在使用 PHP Laravel。
我需要获取每位员工的总时间/小时数。每个员工都有一个分配的 timeKeeping_empID。 Time_Logs 表有以timeKeeping_empID为外键的Employees的时间日志。
我尝试运行我的代码: 控制器:
function getTotalHours(Request $request)
{
$team = Employees::where('managerID', $request->session()->get('employeeID'))->get();
//I am assigning an array to be grouped according to my employeeID session.
$teamIDs = array();
//I am getting my own session so I can see my data as well together with my team
array_push($teamIDs, $request->session()->get('employeeID'));
foreach($team as $member) {
array_push($teamIDs, $member->employeeID);
}
//I am getting the column employeeID which is the same with the teamID
$empData = Employees::whereIn('employeeID', $teamIDs)->get();
$timeKeepingIDs = array();
foreach($empData as $anEmpData) {
array_push($timeKeepingIDs, $anEmpData->timeKeeping_empID);
}
$timeLogData = Time_Logs::whereIn('timeKeeping_empID', $timeKeepingIDs)->get();
return view('welcome', compact('timeLogData','request'));
}
在我看来: $totalHours=0;
foreach ($timeLogData as $timeLogDatas) {
$dateToday = new DateTime($timeLogDatas->date);
$timeFrom = date_create($timeLogDatas->time_in);
$timeTo = date_create($timeLogDatas->time_out);
$hoursRendered = date_diff($timeFrom, $timeTo);
$totalHours=$totalHours += (int) $hoursRendered->format('%h');
}
echo "$totalHours";
但结果是所有员工的总小时数。 我需要的是每个员工的总小时数。
这是我的数据库表:
(Employee)
EmployeeID (primaryKey)
timeKeeping_empID
name
(Time_Logs)
id(primaryKey)
timeKeeping_empID
date
time-in
time-out
对不起,我是 PHP 新手。
【问题讨论】:
-
如果您包含对数据库表的描述会有所帮助
-
另外,当你与每个员工循环时,你应该将总小时数归零。
-
已更新。与数据库表。
-
您可以在查询中使用聚合函数 SUM('YOUR-TIME-COL') 和 GROUP BY timeKeeping_empID
标签: php laravel sorting foreach