39

I have a Subscriber model

// Subscriber Model

id
user_id
subscribable_id
subscribable_type

public function user()
{
    return $this->belongsTo('App\User');
}

public function subscribable()
{
    return $this->morphTo();
}

And a Topic model

// Topic Model

public function subscribers()
{
    return $this->morphMany('App\Subscriber', 'subscribable');
}

And I want to get all users through Subscriber model, to notify them like

Notification::send($topic->users, new Notification($topic));

// Topic Model


public function users()
{
    return $this->hasManyThrough('App\User', 'App\Subscriber');
}

Any ideas?

shaedrich
  • 5,457
  • 3
  • 26
  • 42
Edward
  • 421
  • 1
  • 4
  • 5

4 Answers4

82
// Topic Model

public function users()
{
    return $this->hasManyThrough('App\User', 'App\Subscriber', 'subscribable_id')
        ->where(
            'subscribable_type', 
            array_search(static::class, Relation::morphMap()) ?: static::class
        );
}

Polymorphic hasManyThrough relationships are the same as any others, but with an added constraint on the subscribable_type, which can be retrieved from the Relation::morphMap() array, or by using the class name directly.

Erich
  • 2,408
  • 18
  • 40
Matt Hanley
  • 921
  • 6
  • 5
  • 1
    The answer assumes a different relationship model to the one in question. `hasManyThrough` would require subscriber ID to be present in the users table. – Maksim Ivanov Nov 12 '19 at 08:35
13

In addition to Matt's approach, the following code also could be another solution:

//Topic Model
public function users()
{
    return $this->belongsToMany(User::class, 'subscribers', 'subscribale_id', 'user_id')
        ->where('subscribale_type', static::class);
}

In this way Subscriber treated as a pivot table and second argument is table name for pivot.

The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to. Read more here.

Consider the where clause after belongsToMany to filter only the current model.

Khalil Laleh
  • 1,168
  • 10
  • 19
12

What you're after is not hasManyThrough, but morphedByMany.

hasManyThrough is useful when you have A one-to-many B, B one-to-many C, and you want to use A to fetch many C.

What you have is a polymorphic pivot table sitting in the middle between A and C.

// Topic Model

public function users()
{
    return $this->morphedByMany('App\User', 'subscribable', 'pivot/subscriber_table_name');
}

Docs: https://laravel.com/docs/6.x/eloquent-relationships#many-to-many-polymorphic-relations

Maksim Ivanov
  • 3,991
  • 31
  • 25
  • Why do you think he has a "polymorphic pivot table sitting in the middle" which assumes a polymorphic many to many? It looks more like a polymorphic one to many. – digout Jan 18 '20 at 21:36
  • @digout To me it seems like a typical scenario where a single user can be subscribed to many topics and a single topic can be followed by many users. – Maksim Ivanov Jan 23 '20 at 00:38
5

try this package https://github.com/staudenmeir/eloquent-has-many-deep

then you could use it like this:

class Topic extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
    public function users()
    {
        return $this->hasManyDeep(
            'App\User',
            ['App\Subscriber'],
            [null, ['subscribable_type', 'subscribable_id']]
        );
    }
}
mohamed al-ashry
  • 251
  • 6
  • 17