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

zahedkamal87's avatar

Divide a column by another column created from Sub query

I've this query -

$employees = Employee::where('user_id', $user->id)->withCount([
                            'answers as questions_received' => function ($query) use($date) {
                                $query->where('created_at', '>=', $date);
                            }, 
                            'answers as correct' => function ($query) use($date) {
                                $query->where('correct', 1)->where('created_at', '>=', $date);
                            }
                        ]);

$employees = $employees
                            ->orderBy('answered', 'desc')
                            ->orderBy('correct', 'desc')
                            ->get();

I want to do something like this -

((questions_received/correct) * 100) as percentage

But don't understand how to do it. Please help :)

Thank you

0 likes
0 replies

Please or to participate in this conversation.