Laravel Polymorphic Relations Has Many Through

前端 未结 5 1739
闹比i
闹比i 2020-12-09 09:37

I have a Subscriber Model

// Subscriber Model

id
user_id
subscribable_id
subscribable_type

public function user()
{
    return $this->belongsTo(\'App\\U         


        
5条回答
  •  离开以前
    2020-12-09 10:04

    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

提交回复
热议问题