Laravel Eloquent 中的多对多复杂多态关系
Many To Many Complex Polymorphic Relationship in Laravel Eloquent
我在项目中做通知系统。我有一个数据库,其中有用户和属于用户的客户。可以为用户和客户端添加通知。这就是我使用多对多多态关系的原因,它在一定程度上运作良好。我无法处理为用户建立关系的问题,这将为他及其所有客户接收通知。
用户模型:
public function clients()
{
return $this->hasMany('App\Models\Client');
}
public function notifications()
{
return $this->morphToMany('App\Models\Notification', 'notifiable')
->withPivot('notice_time', 'user_seen')->withTimestamps();
//Normally I would use it but here I will not catch notifications for clients
return \Notification::whereHas('clients', function($q){
$q->where('clients.user_id', $this->id);
})->orWhereHas('users', function($q){
$q->where('users.id', $this->id);
});
//The closest I have been using such code, however in this option I can't use wherePivot()
}
通知模型:
public function clients(){
return $this->morphedByMany('App\Models\Client', $this->pivot)
->withPivot(self::USER_NOTICE_COLUMN, self::USER_SEEN_COLUMN)->withTimestamps();
}
public function users(){
return $this->morphedByMany('App\Models\User', $this->pivot)
->withPivot(self::USER_NOTICE_COLUMN, self::USER_SEEN_COLUMN)->withTimestamps();
}
表格小预览:
Notifiables |Notifications | Clients | Users |
----------------+--------------+---------+--------+
id |id |id |id |
notification_id |title |user_id |email |
notifiable_id |message |name |password|
user_seen | |surname | |
notice_time | | | |
notifiable_type | | | |
是否有可能建立这样的关系,以便我可以使用用户对象的用户通知功能,并且仍然可以使用查询范围?
看来你不能轻易定义这样的关系。从 Notification 开始并加入 Pivot 的最简单方法 table.
public function notifications(){
return \Notification::
whereHas('clients', function($q){
$q->where('clients.user_id', $this->id);
})->orWhereHas('users', function($q){
$q->where('users.id', $this->id);
})->join('notifiables', 'notifications.id', '=', 'notifiables.notification_id');
}
我在项目中做通知系统。我有一个数据库,其中有用户和属于用户的客户。可以为用户和客户端添加通知。这就是我使用多对多多态关系的原因,它在一定程度上运作良好。我无法处理为用户建立关系的问题,这将为他及其所有客户接收通知。
用户模型:
public function clients()
{
return $this->hasMany('App\Models\Client');
}
public function notifications()
{
return $this->morphToMany('App\Models\Notification', 'notifiable')
->withPivot('notice_time', 'user_seen')->withTimestamps();
//Normally I would use it but here I will not catch notifications for clients
return \Notification::whereHas('clients', function($q){
$q->where('clients.user_id', $this->id);
})->orWhereHas('users', function($q){
$q->where('users.id', $this->id);
});
//The closest I have been using such code, however in this option I can't use wherePivot()
}
通知模型:
public function clients(){
return $this->morphedByMany('App\Models\Client', $this->pivot)
->withPivot(self::USER_NOTICE_COLUMN, self::USER_SEEN_COLUMN)->withTimestamps();
}
public function users(){
return $this->morphedByMany('App\Models\User', $this->pivot)
->withPivot(self::USER_NOTICE_COLUMN, self::USER_SEEN_COLUMN)->withTimestamps();
}
表格小预览:
Notifiables |Notifications | Clients | Users | ----------------+--------------+---------+--------+ id |id |id |id | notification_id |title |user_id |email | notifiable_id |message |name |password| user_seen | |surname | | notice_time | | | | notifiable_type | | | |
是否有可能建立这样的关系,以便我可以使用用户对象的用户通知功能,并且仍然可以使用查询范围?
看来你不能轻易定义这样的关系。从 Notification 开始并加入 Pivot 的最简单方法 table.
public function notifications(){
return \Notification::
whereHas('clients', function($q){
$q->where('clients.user_id', $this->id);
})->orWhereHas('users', function($q){
$q->where('users.id', $this->id);
})->join('notifiables', 'notifications.id', '=', 'notifiables.notification_id');
}