Skip to main content

Example 2. Generating a Monthly Report for Employee Work Hours

Premium
4 min read

Comments & Discussion

GK
Geovane Kruger ✓ Link copied!

I noticed that for each select, you added a ->addSelect(), but it's possible to leave everything grouped. These are just choices on how to write the code, right?

At first I'm using it in my code like this:

$tl = TrainingLoadActivity::where('athlete_id', $athleteId)
->select(
DB::raw('DATE(performed_at) as performed_at'),
DB::raw('sum(session_rpe_load) as session_rpe_load'),
DB::raw('sum(compound_load) as compound_load'),
//DB::raw('sum(base_load) as base_load'),
DB::raw('count(*) as trainings_day'),
)
->groupBy('performed_at')
->orderBy('performed_at')
->get();
M
Modestas ✓ Link copied!

You can definitely add them to a single function (this was done for readability and clarity), but keep in mind that it has to be:

->addSelect([]) and not just ->select()

Or in other words:

$tl = TrainingLoadActivity::where('athlete_id', $athleteId)
->addSelect([
DB::raw('DATE(performed_at) as performed_at'),
DB::raw('sum(session_rpe_load) as session_rpe_load'),
DB::raw('sum(compound_load) as compound_load'),
//DB::raw('sum(base_load) as base_load'),
DB::raw('count(*) as trainings_day'),
])
->groupBy('performed_at')
->orderBy('performed_at')
->get();
```
GK
Geovane Kruger ✓ Link copied!

Oops, Thanks for that observation, I didn't know that. At first the code worked perfectly as I had done, not even PHPStan on level 6 showed anything.

But I will correct.

AI
Alexandr Illarionov ✓ Link copied!

Hi there, thanks for the course!

Today, I cloned the repo, ran tests, and got an error in test_employee_timesheet_report.

It happens because of this condition (today is the 1st February):

->where('start', '>=', now()->startOfMonth())

app/tests/Feature/GrouppingExamples/TimesheetReportTest.php:57

Maybe it's not the best solution, you shall travel in time in the begining of function:

$this->travelTo(now()->setDay(15));

Like it was in the lesson Travel in Time to Reproduce Test Scenarios of Advanced Laravel Testing course.

Also, maybe someone already noticed it, in test_the_application_returns_a_successful_response get 302 status because of:

Route::get('/', function () {
return to_route('dashboard');
});

app/routes/web.php:37
Thanks!

M
Modestas ✓ Link copied!

Hi, thank you for your comment! It is always nice to see people using multiple courses to reference things that might be off, thank you for that!

As per the issue - I will have to check and see if any adjustments are needed, but this just proves that working with dates is hard :) What works one day, might not function on a specific race-condition day.

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.