Laravel关系获取用户电子邮件

时间:2019-01-07 10:53:40

标签: php laravel eloquent has-many-through

嘿,我正在从网站上向用户发送多个通知,正在将用户分配到团队中,然后将该团队分配到通知表中。

但是,当我执行SiteNotification::find(1)->notifications()时,我会得到团队的名称,但是,我一直在寻找用户模型以及与此相关的所有详细信息。有没有一种简单的方法可以使用Laravel雄辩的关系来获取此信息?

我的数据库模型和口才模型在下面;

数据库表;

用户

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)

有什么想法我在做什么错吗?

2 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,这意味着我不需要修改现有的数据库结构,并且找到了可以使用的正确关系。

public function users()
    {
        return $this->belongsToMany(
            User::class,
            'team_members',
            'team_id',
            'user_id'
        );

    }

现在我可以Site::find(1)->users->pluck('email')

答案 1 :(得分:-1)

您必须更改模型结构...这就是我要达到的目标...将其作为“可行的解决方案”,也许不是最好的!

首先,数据库。您应该有这些表,无需

users                    => users table
teams                    => teams table
team_user                => pivot table n:n
team_site_notification   => pivot table n:n
site_notifications       => notifications table
user_site_notification   => pivot table n:n

然后创建相关的模型关系

public class User {
    // [...]
    public function teams() {
        return $this->belongsToMany(Team::class)
    }

    public function notifications() {
        return $this->belongsToMany(SiteNotification::class)
    }
}

public class Team {
    // [...]
    public function users() {
        return $this->belongsToMany(User::class)
    }

    public function notifications() {
        return $this->belongsToMany(SiteNotification::class)
    }
}

public class SiteNotification {
    // [...]
    public function teams() {
        return $this->belongsToMany(Team::class)
    }

    public function users() {
        return $this->belongsToMany(User::class)
    }
}

在您的控制器中,创建SiteNotification模型时,您还必须关联用户。例如

public function store(Request $request) {
   // Do your stuff
   $team = Team::findOrFail($request->your_team_id);
   $notification = Notification::create($data);

   $notification->teams()->associate($request->your_team_id);

   // Retrieve the users from the team... Maybe not everyone should receive a notification
   $team->users()->whereIn('id', $user_ids)->get()->pluck('id')
   $notification->users()->associate($ids);
}

要获取用户列表时,您可以通过以下方式简单地检索关联的用户:

dd($notification->users);

// [ User:{id: 1, '...'}, User:{id: 2}, User:{id: 7} ]

希望这就是您想要的!