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

JhonD's avatar
Level 1

how to get users with booking count in laravel without using withcount because there is a bad performence with large database

how to get users with booking count in laravel without using withcount because there is a bad performence with large database is there is any thing best performance than this ?

users::withCount('bookings')->get(); ?
0 likes
2 replies
Nakov's avatar

The bad performance is not coming from using withCount but rather from when you do ->get() because you are trying to do that for ALL the users in the database. So a better performance will be if you paginate your results:

User::withCount('bookings')->paginate(10);

Very well documented here https://laravel.com/docs/8.x/pagination

Snapey's avatar

You need to review the sql query and make sure you have the correct indexes in place.

Do you really need the bookings count for every user?

If so, a better solution might be Booking::groupBy(user_id)

Please or to participate in this conversation.