@BryceSharp I don't think that's correct. I'm almost positive that
withCount should be running a subquery to get the count. It shouldn't call the
@JeffBeltran Are you absolutely certain that the second query is only 350ms? Your second query runs 2 separate queries whereas the first runs a subquery inside a single query.
In addition, what are you trying to achieve? In your first example (
withCount), you can access the number of enrollments easily because it will be appended as an attribute of the parent. Very simple and efficient if all you want is the number of enrollments. However, if you are also trying to get other attributes that belong to enrollment, you want the
with method and may accidentally be running more queries by trying to access enrollments.
In your second example (
with), you can also access the number of enrollments, but you are fetching the entire "enrollment" model. You are getting a collection of these and then essentially appending each enrollment model to its respective parent. You will have access to all enrollment attributes. In addition to this, in order to get the enrollment count, you will need to run the
count() method on the collection instance. You are doing a lot more to have access to a lot more data.