【发布时间】:2019-06-02 00:49:40
【问题描述】:
嘿,从我的网站,我正在向用户发送多个通知,我正在将用户分配到一个团队,然后我将此团队分配到通知表。
但是,当我执行SiteNotification::find(1)->notifications() 时,我得到了团队的名称,但是,我正在寻找用户模型以及与之相关的所有详细信息。有没有一种使用 Laravel Eloquent 关系的简单方法来获得这个?
我的 DB 模型和 Eloquent 模型如下;
数据库表;
用户
id | username | email
团队
id | name |
团队成员
team_id | user_id
网站通知
site_notification_id | team_id
模型在这里:
class SiteNotification extends Model {
public function notifications()
{
return $this->belongsToMany(Team::class, 'site_check_notifications', 'site_check_id', 'team_id');
}
}
更新:
我尝试如下更新团队模型;
class Team extends Model
{
public function users()
{
return $this->hasManyThrough(
User::class,
TeamMember::class,
'team_id',
'id'
);
}
}
但是在运行这个时会抛出如下错误;
$site = Site::find(1);
foreach( $site->notifications as $notification) {
dd($notification->users);
}
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'team_members.id' in 'on clause' (SQL: select `users`.*, `team_members`.`team_id` from `users` inner join `team_members` on `team_members`.`id` = `users`.`id` where `team_members`.`team_id` = 4)
任何想法我做错了什么??
【问题讨论】:
-
我不明白...您要检索与团队相关的用户,与通知相关的用户或已通知的用户?
-
@IlGala 是的,我想检索与通知相关的用户
标签: php laravel eloquent has-many-through