1

I have some tables like these:

users, followers_pivot, activities

In my User model, all relations are set and working correctly.

Some methods from my User.php model:

class User extends Eloquent {

    //The ones I follow
    public function imFollowing() {
        return $this->belongsToMany('User', 'followers_pivot', 'follower_id', 'followee_id');
    }

    //The people who's following me
    public function followers() {
        return $this->belongsToMany('User', 'followers_pivot', 'followee_id', 'follower_id');
    }

    //shows all activities of the user
    public function activities() {
        return $this->hasMany('Activity', 'userID');
    }
}

I want to fetch activities of everyone I'm following.

I can fetch this way:

User::with('imFollowing.activities')->find(11);

But, it's not enough. They are under imFollowing collection, and separated by each user.

I want to fetch activities directly and not separated under imFollowings.

I thought of a Has-Many-Through relationship, but I couldn't put it together.

This is what I've tried using hasManyThrough:

//User.php Model
public function activityFeed() {
    return $this->hasManyThrough('Activity', 'User', 'id', 'userID');
}

And

//in a route
return User::with('activityFeed')->find(11);

But this returns the collection null.

Edit: I can do this using Fluent way:

Activity::join('users', 'activities.userID', '=', 'users.id', 'inner')
    ->join('followers_pivot', 'followers_pivot.followee_id', '=', 'users.id', 'inner')
    ->where('followers_pivot.follower_id', 11)
    ->orderBy('activities.id', 'desc')
    ->select('activities.*')
    ->get();

How can I achieve this using Eloquent? This will be only place where I'll be using Fluent, and I'm not quite satisfied with it. Is the polymorphic approach better for this? If so, how?

Thanks in advance,

Arda
  • 6,756
  • 3
  • 47
  • 67
  • 2
    `Eloquent` doesn't provide any relation for this, but this will help you http://stackoverflow.com/questions/23788844/hasmanythrough-with-on-to-many-relationship/23789210#answer-23789210 Ask if you find anything unclear – Jarek Tkaczyk Jul 03 '14 at 13:53
  • @deczo Thanks, your approach in that answer is quite great! Although it still uses Fluent in a way, it'll be way cleaner than a fluent query at least. – Arda Jul 03 '14 at 14:19
  • 1
    You know, `Eloquent` is Fluent in the end. `Eloquent\Builder` uses base `Query\Builder`. The only thing I would change to what you can find in that answer, is to use getters instead of hard-coding table names etc. You can rely on `$this->relation()->getTable()` and such methods. – Jarek Tkaczyk Jul 03 '14 at 14:40
  • @deczo Yes I know, but the thing is that I just don't want to use table names inside models except `protected $table = 'table_name';` – Arda Jul 03 '14 at 14:47
  • 1
    I see, then do as I suggested in my previous comment :) – Jarek Tkaczyk Jul 03 '14 at 15:07

1 Answers1

0

I was also having the same doubt. As this is the first google answer I will post the answer here:

As explained here Has many through self referencing in laravel returns empty set

It's not a HasManyThrough relationship, it's a BelongsToMany relationship!

RicardoPHP
  • 492
  • 3
  • 10