Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ajsmith_codes's avatar

Break attendance report into pay periods.

I can't seem to figure out the best way to do this. Each employee punches in and out daily. The punch record shows employee_num, date, time in, time out, etc.

Is there an easy way to break the data into pay periods? We start ours the first week of the year and go 14 days.

Currently, I have to get each pay period separately:

    $weekOne = $this->getWeekOne($employee, $weekOneStartDate, $weekOneEndDate);
        $weekOneTotal = $weekOne->sum('total_time_converted');
        $weekOnePto = $weekOne->sum('pto');
        $weekOneOt = $weekOne->sum('overtime');
        $weekOneHol = $weekOne->sum('holiday');
        $weekOneGrandTotal = $weekOneTotal + $weekOnePto + $weekOneOt + $weekOneHol;

        $weekTwo = $this->getWeekTwo($employee, $weekTwoStartDate, $weekTwoEndDate);
        $weekTwoTotal = $weekTwo->sum('total_time_converted');
        $weekTwoPto = $weekTwo->sum('pto');
        $weekTwoOt = $weekTwo->sum('overtime');
        $weekTwoHol = $weekTwo->sum('holiday');
        $weekTwoGrandTotal = $weekTwoTotal + $weekTwoPto + $weekTwoOt + $weekTwoHol;
0 likes
23 replies
ajsmith_codes's avatar

@jlrdw Currently I don't have which pay period the punch belongs to so I don't think that would work for this.

ajsmith_codes's avatar

@jlrdw I haven't only because I'm already pretty far into the coding. Also, I hear it's not a great idea to use too many packages and I'm already using quite a few.

ajsmith_codes's avatar

@MohamedTammam I'm trying to apply that to my query in Laravel, but not sure how.

     $employeePunches = DB::table('employee_punches')
            ->where('employee_num', $employee->employee_num)
            ->get();
MohamedTammam's avatar

@ajsmith_codes

$employeePunches = DB::table('employee_punches')
            ->where('employee_num', $employee->employee_num)
			->groupBy(DB::raw('WEEK(time_in)'))
            ->get();
ajsmith_codes's avatar

@MohamedTammam I get this error:

"SQLSTATE[42000]: Syntax error or access violation: 1055 'laravel.employee_punches.id' isn't in GROUP BY (SQL: select * from `employee_punches` where `employee_num` = 3 group by WEEK(date))"

The punches are by date with a time in and time out, so I changed the group to date.

MohamedTammam's avatar

@ajsmith_codes Please try.

$employeePunches = DB::table('employee_punches')
			->select('*', DB::raw('WEEK(date) as week')
            ->where('employee_num', $employee->employee_num)
			->groupBy('week')
            ->get();
ajsmith_codes's avatar

@MohamedTammam I am trying that but it's not liking the code. The error for "groupBy" is:

Method "groupBy" not found in \Illuminate\Database\Query\Expression
ajsmith_codes's avatar

Let's change this a bit. How would we group it or display by week in the view?

ajsmith_codes's avatar

Finally figured it out. After looking at Stack Overflow, it appears I needed to do this:

      return  DB::table('employee_punches')
            ->where('employee_num', $employee->employee_num)
            ->select(DB::raw('SUM(pto) AS pto'),DB::raw('SUM(overtime) AS overtime'),DB::raw('SUM(total_time_converted) AS total_time_converted'), DB::raw('MONTH(date)'))
            ->groupBy(DB::raw('MONTH(date)'))
            ->get();

It's a little different than what was posted before, but everyone was really close.

ajsmith_codes's avatar

@jlrdw Yes, but I didn't realize I had to repeat the code in the select and the groupBy.

Now, how do I display it in the view? This doesn't work:

       @foreach($punches as $punch)
                        <td>
                            {{ $punch }}
                        </td>
                    @endforeach

I also tried:

                    @foreach($punches as $punch)
                        <td>
                            {{ $punch }}
                        </td>
                    @endforeach

EDIT: These are my results if I return them without using the foreach:

[
{
"pto": "0.00",
"overtime": "16.00",
"total_time_converted": "40.00",
"holiday": "0.00",
"WEEK(date) DIV 2": 0
},
{
"pto": "0.00",
"overtime": "31.00",
"total_time_converted": "80.00",
"holiday": "0.00",
"WEEK(date) DIV 2": 1
},
{
"pto": "1.00",
"overtime": "31.00",
"total_time_converted": "80.00",
"holiday": "0.00",
"WEEK(date) DIV 2": 2
},
]
jlrdw's avatar
@foreach($punches as $punch)
    <td>
           {{ $punch->pto }}
    </td>
    <td>
           {{ $punch->overtime }}
    </td>
  etc
@endforeach
ajsmith_codes's avatar

@jlrdw That is a tough choice. Both of you helped. I will mark MohammedTammam's because his reply had example code that was very close.

Please or to participate in this conversation.