【问题标题】:Laravel relationships get user emailsLaravel 关系获取用户电子邮件
【发布时间】: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


【解决方案1】:

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

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

    }

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

【讨论】:

    【解决方案2】:

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

    首先,数据库。你应该有这些表,没有必要

    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} ]
    

    希望这就是你要找的东西!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-23
      • 2019-12-22
      • 2019-01-07
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多