Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 具有两个外键的两个表之间的Laravel关系_Php_Postgresql_Laravel_Eloquent_Psql - Fatal编程技术网

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

Php 具有两个外键的两个表之间的Laravel关系,php,postgresql,laravel,eloquent,psql,Php,Postgresql,Laravel,Eloquent,Psql,嘿,我如何在两个表之间建立关系 Users: id, email Notification: id, user_id(id of the logged user), client_id(id of sender) 我想通过用户id和客户端id建立用户和通知之间的关系。 然后,我将可以得到所有通知分配给登录用户,并获得电子邮件的发件人用户 我提出: public function notifications_with_client() { return $this->has

嘿,我如何在两个表之间建立关系

Users: id, email
Notification: id, user_id(id of the logged user), client_id(id of sender)
我想通过用户id和客户端id建立用户和通知之间的关系。 然后,我将可以得到所有通知分配给登录用户,并获得电子邮件的发件人用户

我提出:

    public function notifications_with_client() {
    return $this->hasManyThrough('App\Models\User', 'App\Models\Notification', 'user_id', 'id', 'client_id');
}
但是,当我使用查询时,我收到了很好的通知,但收到了错误的电子邮件。 我收到了来自关系id(来自用户表)==id(来自通知表)的电子邮件

我的问题

$column = 'notifications_with_client';
$value[1] = ['email', 'notifications.id', 'client_id'];
$query->with([$column => function($query) use ($value) {
                      $query->select($value[1]);
                  }]);
有人知道我做错了什么吗

public function user()
{
    return $this->belongsTo(Users::class, 'user_id');
}

public function client()
{
    return $this->belongsTo(Users::class, 'client_id');
}
在通知模型中使用此代码,您可以使用

$this->user(); // $notification->user();
而发送者

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

您不能使用
$this->hasManyThrough()。
它用于

您可以像这样使用
$this->belongsTo()

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

    public function client()
    {
        return $this->belongsTo(Notification::class, 'client_id');
    }
}
然后你可以像这样查询

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


您可以通过定义以下关系来尝试:

用户
型号

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

public function from()
{
  return $this->belongsTo('App\Models\User', 'client_id');
}
通知
型号

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

public function from()
{
  return $this->belongsTo('App\Models\User', 'client_id');
}
然后您可以按以下方式查询:

$notifications = auth()->user()->notifications()->with('from')->get();
$notifications = auth()->user()
                    ->notifications()
                    ->with(['from' => function($q) {
                        $q->select('email');
                    }])
                    ->get();
或者,如果您只需要
电子邮件
,请将其查询为:

$notifications = auth()->user()->notifications()->with('from')->get();
$notifications = auth()->user()
                    ->notifications()
                    ->with(['from' => function($q) {
                        $q->select('email');
                    }])
                    ->get();

我认为你需要建立多对多的关系?如果你能公布你的这种关系的模式,这将是非常有帮助的。
client\u id
引用了什么?我假设是用户。如果是这样的话,那么你确实有多对多的关系。你不能在这里使用
$this->hasManyThrough()
,正如我在下面的回答中提到的,使用
$this->belongesTo()