Question

Alright, so I have a feed-based web app, kind of like facebook's News Feed. The issue is in the way I am generating the feed. I'm not sure it's the most efficient and/or best way to do it.

I use Laravel + Eloquent ORM

$feed = Post::whereIn('id', $following_ids)
                        ->orderBy('created_at', 'desc')
                        ->take(20)
                        ->skip($page)
                        ->get();

Where $following_ids is an array of ids that the user is following.

The issue is that as I understand it, the SQL generated by the above statement causes every post by the users you're following to be loaded first, and then sorted chronologically only to have a small subset actually used.

Is there any more efficient way to do this? This seems really wasteful.

Was it helpful?

Solution

You should have a look at this package that I wrote: https://github.com/GetStream/stream-laravel; it integrates with your Eloquent models and stores the data in feeds.

With the same package you can then let users follow other feeds and create a feature similar to Facebook's newsfeed (or Twitter).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top