Laravel join with 3 Tables
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Retrieving Feed Posts from Followed Users in Laravel Applications
Introduction
As you build your Twitter-like app, correctly displaying feed posts according to the principle of "Followers' Shares" can be a challenge. This task requires joining multiple tables and ensuring that only the relevant data is retrieved efficiently. In this blog post, we will show you how to achieve this using the Laravel ecosystem, providing examples and best practices along the way.
The Solution: A Comprehensive Answer
First, let's examine your existing code and identify where things might be going wrong:
$shares = DB::table('shares')
->leftjoin('followers', 'shares.user_id', '=', 'followers.follower_id')
->leftjoin('users', 'followers.user_id', '=', 'users.id')
->where('users.id', 3)
->where('shares.user_id', 'followers.follower_id')
->get();
This code is almost correct but suffers from two major issues:
1. Joining the users table after left joining with followers can lead to incorrect results, as it may include additional unrelated records.
2. Applying the 'where' condition on both user_id and follows.follower_id will only retrieve posts where the user is sharing their own content, not those shared by others they follow.
A More Effective Approach
To solve these issues, we can rewrite your query using a combination of relationships and joins. Firstly, you should create a relationship between the Share and User models:
class Share extends Model {
public function user() {
return $this->belongsTo('App\User');
}
}
Next, we will leverage Eloquent relationships to perform the joins. Here is a new approach using eager loading:
$followedUsers = User::with(['shares' => function($query) {
$query->where('user_id', '!=', auth()->id()); // ensures we only fetch shares from users other than the current logged in user
}])
->get()
->map(function($poster) use ($followedUsers) {
return $followedUsers->find($poster['user_id']);
})
->filter(function($followedUser) {
return $followedUser->followers()
->where('follower_id', auth()->id())
->exists(); // checks if the authenticated user is following this followedUser
});
This code retrieves all users the authenticated user follows. It then eagerly loads their shares and filters out any shares that are self-posts or not shared by other users you follow. The resulting array will contain only the desired feed posts from the users you follow.
Additional Optimizations
To further improve performance, we suggest using a "followed_users" model to store the relationships between users and their followers, as well as the relationship between shares and their users. This will enable Laravel's built-in caching facilities for improved efficiency.
Conclusion
By adopting a new approach that incorporates Eloquent relationships and eager loading, you can solve the problem of retrieving feed posts from followed users efficiently in your Twitter-like app. This technique ensures that only relevant data is fetched and eliminates redundant queries for better performance. For more information on querying with Laravel, be sure to check out our comprehensive resources at https://laravelcompany.com/blog/.