Php Laravel关系获取用户电子邮件
嘿,在我的网站上,我向用户发送多个通知,我将用户分配给一个团队,然后我将这个团队分配给通知表 然而,当我做Php Laravel关系获取用户电子邮件,php,laravel,eloquent,has-many-through,Php,Laravel,Eloquent,Has Many Through,嘿,在我的网站上,我向用户发送多个通知,我将用户分配给一个团队,然后我将这个团队分配给通知表 然而,当我做SiteNotification::find(1)->notifications()时,我得到了团队的名称,然而,我希望得到用户模型以及与此相关的所有详细信息。有没有一个简单的方法来获得这一点,使用拉威尔雄辩的关系 下面是我的DB模型和雄辩模型 数据库表 用户 id | username | email site_notification_id | team_id 团队 id | nam
SiteNotification::find(1)->notifications()
时,我得到了团队的名称,然而,我希望得到用户模型以及与此相关的所有详细信息。有没有一个简单的方法来获得这一点,使用拉威尔雄辩的关系
下面是我的DB模型和雄辩模型
数据库表
用户
id | username | email
site_notification_id | team_id
团队
id | name |
team_id | user_id
团队成员
id | name |
team_id | user_id
站点通知
id | username | email
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)
你知道我做错了什么吗???你必须改变模型结构。。。这就是我如何达到你的目标。。。把它当作“有效的解决方案”,也许不是最好的 首先是数据库。你应该有这些桌子,没有必要
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)
}
}
在控制器中,当您创建站点通知
模型时,您还必须关联用户。比如说
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->pull('email')
我不明白。。。是否要检索与团队、通知或已通知的用户相关的用户?@IlGala yes我要检索与通知相关的用户