It probably would be best with storing the session count in the database table.
Jul 15, 2015
2
Level 3
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!
Please or to participate in this conversation.