Group by and Sum of One-To-Many relation tables in Eloquent

  eloquent, eloquent--relationship, laravel, mysql

I have a requirement.
My DB has tables like the following.
The tables have OneToMany (1-n) parent-child relation.

Table School (id, school_name)

Table Class (id, school_id, class_name)

Table Section (id, class_id, section, no_of_seats)

Table Student (id, section_id, student_name, ....)

When Some Student is registered, data is uploaded to the Student table.
Now, I want to have a statistic like

| school_name | total_seats | student_registered |

and for a particular school

| class_name | total_seats | student_registered |

How to achieve this in Laravel/Eloquent

Thanks in Advance

Source: Laravel

Leave a Reply