Aug 18, 2021
0
Level 41
Improving this eloquent query ?
Hi,
Here is the context, employees have worksheets. When worksheets are completed, every month the admin will make a recap document to pay them.
I'd like to have a dashboard with only employees that need to be paid (and with some informations about the amount etc).
Here is the Eloquent query I've made, it works but I'm not sure if it's the best solution here.
$employees = Employee::whereHas('worksheets', function ($q) {
$q->toBill();
})->withCount([
'worksheets as total_worksheets' => function ($q) {
$q->toBill();
},
'worksheets as total_points' => function ($q) {
$q->select(DB::raw('SUM(total_points)'))->toBill();
},
'worksheets as total_price' => function ($q) {
$q->select(DB::raw('SUM(total_price)'))->toBill();
}
])->get();
How could I improve this eloquent query ?
Please or to participate in this conversation.