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

danimohamadnejad's avatar

total count and recent data in one query?

Hello. how can I get recent models and also total number of rows using same query? is that possible? thank you in advance

0 likes
5 replies
Nakov's avatar

Yes, it should be possible, but you need to show some code..

Here is an example:

User::with('posts')->withCount('posts')->get();
danimohamadnejad's avatar

thank you for reply. In statistics page of my application I want to have a list of recent users and also I want to show total number of users. there are others things too like total number of orders and recent orders. you know general information. Im trying to reduce number of queries since I want to display data from multiple tables.

danimohamadnejad's avatar

honestly I dont have any code I am just figuring out how to write it. I dont want to get any relation data are toataly separate.

jlrdw's avatar

If you just follow the documentation and write the queries as the examples in the background the correct queries will take place.

And no it will not be just one query it will be at least two if you're pulling in relations also.

But eloquent takes care of that for you.

Please or to participate in this conversation.