1
In Laravel 4, we do not have the method present in Laravel 5.2 >= called withCount
. That one withCount
is responsible for adding to SELECT
the counting of items related to the current entity.
For example:
$u = Usuario::withCount('posts')->first();
var_dump($u->posts_count); // int(5)
But in my case, I am maintaining a system that I did some 3 years ago, where I still used the Laravel 4. And the solution that we had at that time, without writing SQL in hand, was to call a count
for each related item.
Thus:
foreach($usuarios as $usuario) {
echo $usuario->posts()->count();
}
The problem with that is that for every iteration, Laravel will call a SELECT COUNT(*) FROM posts WHERE usuario_id = ?
, and I imagine that in a 500-line iteration, it will screw up my database server.
And in my case, I also believe it is impossible to migrate the project from Laravel 4 to 5.
So I ask you:
- How can I load in advance (Eager Loader) the count of items related to an entity (model) in Laravel 4?
Do you need to load this counter from all users on a given page? Otherwise you could page and load X results with each interaction. In case you need to load everything, which?
– Darlei Fernando Zillmer
@Darleifernandozillmer create an attribute to save in white violates the rules of normalization. I would like a solution close to the
withCount
even– Wallace Maxters
This you can solve with eagerload. Usuario::with('posts')->get();
– cau
@cau not. That brings up all the records, and when I had 10,000 records in the relationship screwed up my application (not to mention that doing so is a no-size gambiarra)
– Wallace Maxters
This worked in Laravel 5. I haven’t tried in 4. $users = User::Join('posts', 'posts.user_id', '=', 'users.id')->groupBy('users.id')->get(['users.id', 'users.name', DB::raw('Count(posts.id) as posts')]); Jeffrey Way: https://laracasts.com/discuss/channels/eloquent/eloquent-query-with-count-of-objects-in-a-one-to-many-relationship
– cau