如何属于两个键,一个通过另一个关系

时间:2019-05-22 07:08:32

标签: laravel orm eloquent foreign-keys

sql方案:

用户

id (ai)

公告

id (ai)
owner = user.id

对话

id (ai)
owner_id = user.id //UPDATED
bulletin_id = bulletins.id // this I can't get user ID(this is another 2nd user!) - don't know how.

会话模型:

public function bulletin()
{
    return $this->belongsTo('App\Bulletins','bulletin_id');
}

public function users()
{
    return $this->belongsTo('App\User','owner_id');
}

我需要 users()返回两个关系

owner_id = user.id and bulletin_id->owner = user.id

相同,但在用户模型user->对话中:

public function conversations()
{
    return $this->hasMany(Conversations::class,'owner_id');
}

在这里我需要所有对话,其中owner_id或bulletin-> owner = user.id

0 个答案:

没有答案