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

amitsolanki24_'s avatar

DB query improvement for Notifications grouping

Hey All,

How can i optimize my below DB query and i also want paginated data.

Output: User A and 46 others likes your post.

0 likes
4 replies
Merklin's avatar

@Tray2 Won't it be fast if there is a relation made between the 2 models?

Tray2's avatar

@Merklin Depends on what you mean with relation, they should have foreign key constraints.

I wasn't referring to a speed increase in my suggestion, I was trying to make it more readable, and to understand the need.

1 like
amitsolanki24_'s avatar

@Tray2

So basically you want the latest like and a count of the likes to a post?

Previously I displayed post like notification to post author separately like

Notification 1. Harry likes your post.
Notification 2. Alex likes your post
And so on.

but now I want to display in group like

Harry and 55 others likes your post.
 Or
Harry and Alex likes your post.

And i want to display latest user name on notification.

Please or to participate in this conversation.