leon13
199
2
Laravel

Sibling count

Posted 4 years ago by leon13

I have an eloquent model Booking which has many Session models.

When I output a a session, or list of sessions, I often need the total number of sessions in that same booking. What's the best way to go about this?

$session->booking->sessions()->count() would create 2 extra queries per Session object.

I know that eager-loading would reduce queries, but I just want to know if I'm fundamentally missing a trick. I've also considered storing the session count in the booking table, but thought maybe it was bad practice.

Thanks!

Please sign in or create an account to participate in this conversation.