Skip to content
Advertisement

Counting related rows in a child table

I have been trying to do some queries and getting a count on related tables using eloquent.

Tables:

  • requests
  • contact (belongs to requests)
  • history (belongs to contact)

As such X number of requests each have Y number of contacts which in term each have Z number of histories

Using sql I can do something like this to get all the counts.

Above Query

But I am a bit lost when using eloquent to build queries. If for instance I was selecting all contacts for a given request at what point would I join/count the history? Or do I need to add in some accessor’s into the relevant Models for these 3 tables?

Thanks in advance for any help.

Advertisement

Answer

You can use helper relation for this, if you’d like to use Eloquent instead of manual joins:

The same would go for Contact model towards History model.

For counting far relation (Request -> History) you can use hasManyThrough relation with a little adjustment.

This way you can eager load those aggregates for multiple models without n+1 issue, nice and easy:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement