I want to ask how to get the total days to 30 days. Then after getting a total value of 30 days, the total attendance is sought. The flow that I caught (total days - attendance).
My model:
public function total_hari($id)
{
$from = date("Y-m-26", strtotime("-1 months"));
$to = date('Y-m-d');
$absen = DB::table('working_schedule')
->select(DB::raw('count(*) as total'))
->where('karyawan_id', '=', $id)
->whereNull('deleted_at')
->where('date', '<=', $to)
->where('date', '>=', $from)
->get();
return ($absen[0]->total);
}
public function alpha_bulan_ini($id)
{
$from = date("Y-m-26", strtotime("-1 months"));
$to = date('Y-m-d');
$total_hari = $this->total_hari($id);
$absen = DB::table('karyawan_absensi')
->select(DB::raw('count(*) as alpha'))
->where('karyawan_id', '=', $id)
->whereNull('deleted_at')
->where('tanggal', '<=', $to)
->where('tanggal', '>=', $from)
->get();
$total_hari = $total_hari ?? 0;
$absen = isset($absen[0] ) ? $absen[0]->alpha : 0;
//return ($total_hari-$absen);
return ($total_hari);
}
My index.blade:
<td style='text-align:center'>{{ isset($key->absensihari) ? $key->total_hari($key->karyawan_id) : ''}}</td>
<td style='text-align:center'>{{ isset($key->absensihari) ? $key->alpha_bulan_ini($key->karyawan_id) : ''}}</td>
My table working_schedule:
id | karyawan_id | date | working_id | type_id | mass_id | created_by | updated_by | deleted_by | created_at | updated_at | deleted_at |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | 1113 | 2019-10-01 | 1 | (NULL) | (NULL) | (NULL) | (NULL) | (NULL) | (NULL) | (NULL) | |
2 | 42 | 2019-05-26 | 4 | (NULL) | (NULL) | (NULL) | 1 | (NULL) | 2019-10-22 23:43:03 | 2019-10-22 23:43:03 | |
3 | 42 | 2019-05-27 | 5 | (NULL) | (NULL) | (NULL) | 1 | (NULL) | 2019-10-22 23:43:07 | 2019-10-22 23:43:07 | |
4 | 42 | 2019-05-28 | 3 | (NULL) | (NULL) | (NULL) | 1 | (NULL) | 2019-10-22 23:43:11 | 2019-10-22 23:43:11 | |
5 | 42 | 2019-05-29 | 4 | (NULL) | (NULL) | (NULL) | 1 | (NULL) | 2019-10-22 23:43:14 | 2019-10-22 23:43:14 |
My Answer:
Y-m-d", strtotime("-30 days")); $to = date("Y-m-d"); $total_days = DB::table('attendances')->where('user_id', $id)->whereBetween('date', [$from, $to])->count(); $total_attendance = DB::table('attendances')->where('user_id', $id)->whereBetween('date', [$from, $to])->where('status', 'present')->count(); return ['total_days' => $total_days, 'total_attendance' => $total_attendance];}_In this model, I first calculate the total days by counting the number of attendance records within the last 30 days for a specific user. Then, I calculate the total attendance by counting the number of 'present' status records within the same time frame. Finally, I return an array containing the total days and total attendance values.
Rate this post
4 of 5 based on 7324 votesComments