Laravel Polymorphic Relations Has Many Through

前端 未结 5 1749
闹比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 09:56

    // 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.

提交回复
热议问题