Laravel 关系获取用户电子邮件
Laravel relationships get user emails
嘿,我在我的网站上向用户发送了多个通知,我将用户分配给一个团队,然后我将这个团队分配给通知 table。
但是,当我执行 SiteNotification::find(1)->notifications()
时,我得到了团队的名称,但是,我希望获得用户模型以及与之相关的所有详细信息。有没有一种简单的方法可以使用 Laravel Eloquent 关系来获得它?
我的数据库模型和Eloquent模型如下;
DB tables;
用户
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'
);
}
}
但是当运行 this;
$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)
知道我做错了什么吗??
你必须改变模型结构......这就是我达到你的目标的方式......把它当作"working solution",也许不是最好的!
首先,数据库。这些表你应该有,没必要
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} ]
希望这就是您要找的!
我找到了一个解决方案,这意味着我不需要修改我现有的数据库结构,而且我找到了要使用的正确关系。
public function users()
{
return $this->belongsToMany(
User::class,
'team_members',
'team_id',
'user_id'
);
}
现在我可以做到了Site::find(1)->users->pluck('email')
嘿,我在我的网站上向用户发送了多个通知,我将用户分配给一个团队,然后我将这个团队分配给通知 table。
但是,当我执行 SiteNotification::find(1)->notifications()
时,我得到了团队的名称,但是,我希望获得用户模型以及与之相关的所有详细信息。有没有一种简单的方法可以使用 Laravel Eloquent 关系来获得它?
我的数据库模型和Eloquent模型如下;
DB tables;
用户
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'
);
}
}
但是当运行 this;
$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)
知道我做错了什么吗??
你必须改变模型结构......这就是我达到你的目标的方式......把它当作"working solution",也许不是最好的!
首先,数据库。这些表你应该有,没必要
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} ]
希望这就是您要找的!
我找到了一个解决方案,这意味着我不需要修改我现有的数据库结构,而且我找到了要使用的正确关系。
public function users()
{
return $this->belongsToMany(
User::class,
'team_members',
'team_id',
'user_id'
);
}
现在我可以做到了Site::find(1)->users->pluck('email')