小编Edw*_*ard的帖子

Laravel多态关系有很多

我有一个订阅者模型

// Subscriber Model

id
user_id
subscribable_id
subscribable_type

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

public function subscribable()
{
    return $this->morphTo();
}
Run Code Online (Sandbox Code Playgroud)

和一个主题模型

// Topic Model

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

我希望让所有用户通过订阅者模型,通知他们

通知::发送($ topic-> users,new Notification($ topic));

// Topic Model


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

有任何想法吗?

php relationship has-many-through laravel

9
推荐指数
4
解决办法
3357
查看次数

标签 统计

has-many-through ×1

laravel ×1

php ×1

relationship ×1