dongpao1083
2017-01-13 15:45
浏览 176
已采纳

两个表之间的Laravel关系有两个外键

Hey how can I make relationships between two table.

Users: id, email
Notification: id, user_id(id of the logged user), client_id(id of sender)

I would like make relationship between users and notifications by user_id and client_id. Then I will can get all notifications assigned to logged user, and get email of sender users.

I made that:

    public function notifications_with_client() {
    return $this->hasManyThrough('App\Models\User', 'App\Models\Notification', 'user_id', 'id', 'client_id');
}

But when I using query i got good notifications but with wrong email. I got email from ralationship id(from users table) == id(from notifications table)

My query

$column = 'notifications_with_client';
$value[1] = ['email', 'notifications.id', 'client_id'];
$query->with([$column => function($query) use ($value) {
                      $query->select($value[1]);
                  }]);

Someone know what I do wrong?

图片转代码服务由CSDN问答提供 功能建议

嘿我怎样才能在两个表之间建立关系。

  用户:id,email 
Notification:id,user_id(已登录用户的ID),client_id(发件人ID)
   
 
 

我想在用户之间建立关系 通过user_id和client_id发出通知。 然后,我可以将所有通知分配给已登录用户,并获取发件人用户的电子邮件。

我做到了: < pre> public function notifications_with_client(){ return $ this-&gt; hasManyThrough('App \ Models \ User','App \ Models \ Notification','user_id','id','client_id'); }

但是当我使用查询时,我收到了很好的通知但电子邮件错误。 我从ralationship id(来自users表)收到了电子邮件== id( 来自通知表)

我的查询

  $ column ='notifications_with_client'; 
 $ value [1] = ['email'  ,'notifications.id','client_id']; 
 $ query-&gt; with([$ column =&gt; function($ query)use($ value){
  $ query-&gt; select($ value [1]); 
}]); 
   
 
 

有人知道我做错了吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongpu2694 2017-01-13 16:25
    已采纳

    You can try it by defining the following relations:

    User Model

    public function notifications()
    {
        return $this->hasMany('App\Models\Notification');
    }
    

    Notification Model

    public function to()
    {
      return $this->belongsTo('App\Models\User', 'user_id');
    }
    
    public function from()
    {
      return $this->belongsTo('App\Models\User', 'client_id');
    }
    

    And then you can query it as:

    $notifications = auth()->user()->notifications()->with('from')->get();
    

    Or if you just want email then query it as:

    $notifications = auth()->user()
                        ->notifications()
                        ->with(['from' => function($q) {
                            $q->select('email');
                        }])
                        ->get();
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doukong1901 2017-01-13 16:13
    public function user()
    {
        return $this->belongsTo(Users::class, 'user_id');
    }
    
    public function client()
    {
        return $this->belongsTo(Users::class, 'client_id');
    }
    

    With this code in your Notification Model you can get the logged user with

    $this->user(); // $notification->user();
    

    and the sender with

    $this->client(); //$notification->client();
    
    评论
    解决 无用
    打赏 举报
  • duanliao5995 2017-01-13 16:21

    You cannot use $this->hasManyThrough(). It use for a different reason

    You can use $this->belongsTo() like this.

    class User extends BaseModel
    {
        public function user()
        {
            return $this->belongsTo(Notification::class, 'user_id');
        }
    
        public function client()
        {
            return $this->belongsTo(Notification::class, 'client_id');
        }
    }
    

    Then you can query like.

    User::with(['user']);
    

    or

    User::with(['client']);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题