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

leon13's avatar

Sibling count

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!

0 likes
2 replies
brti's avatar

It probably would be best with storing the session count in the database table.

leon13's avatar

The more I think about it, the more I agree.

Please or to participate in this conversation.