Have you browsed the eloquent docs on Taylor's examples of a one to many https://laravel.com/docs/5.6/eloquent-relationships#one-to-many
Perhaps you could work the example, try some code and then if stuck ask for help.
Me, I'd use query as is.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
How can I write that query in Laravel?
SELECT
parent.id,
COUNT(*) AS childs
FROM
categories AS parent
INNER JOIN categories AS child ON
child.parent_id=parent.id
WHERE parent.`parent_id` = 1
GROUP BY
parent.id
Thanks!
Please or to participate in this conversation.