Laravel ORM与键上的条件有很多关系

时间:2021-02-20 20:13:58

I have a members table like this:

我有一个像这样的成员表:

------------------
| id | name | ...
------------------
   1   John
   2   Jane
   3   Bill

and a relations table to store their relationship.

以及存储他们关系的关系表。

----------------------------
| from_id | to_id | status |
----------------------------
   1          2     friend
   2          3     friend

first records says: member#1 and member#2 are friends. member#1 is friend with #2 And member#2 is friend with #1.

第一条记录说:成员#1和成员#2是朋友。成员#1是#2的朋友而成员#2是#1的朋友。

in Laravel, How can I have a relation to get all friends of a member?
I need to check both columns from_id and to_id , and with this syntax in my Member model I can't get it.

在Laravel,我如何才能获得会员的所有朋友?我需要检查from_id和to_id这两个列,并且在我的Member模型中使用这种语法我无法得到它。

Member Model

public function friends(){
    return $this->belongsToMany('Member', 'relations', 'from_id', 'to_id' );
}

this only returns records that member_id is from_id

这只返回member_id是from_id的记录

I want to have this query , if member = 1

如果member = 1,我想要这个查询

SELECT * FROM relations WHERE from_id = 1 OR to_id = 1

any help?

thanks.

1 个解决方案

#1


Well, this is not the right answer to my question by Using Laravel ORM, but I'm using this and it produces the same result. I hope if someone else knows the correct answer share us here. thanks.

好吧,这不是我使用Laravel ORM的问题的正确答案,但我使用它并产生相同的结果。我希望如果其他人知道正确的答案在这里分享我们。谢谢。

Define this method in Member model:

在Member模型中定义此方法:

return Member::join('relations', function($join) use($limit) {
            $join->on('relations.from_id', '=', 'members.id')
                ->orOn('relations.to_id', '=', 'members.id');
        })
        ->where(function($where){
            $where->where('relations.from_id', '=', $this->id)
                ->orWhere('relations.to_id', '=', $this->id);
        })
        ->where('members.id', '!=', $this->id)
        ->groupBy('members.id')
        ->get();

So I can use:

所以我可以用:

$member = Member::find(1); // for example user#1
$member->friends(); // a list of eloquent objects of members.

#1


Well, this is not the right answer to my question by Using Laravel ORM, but I'm using this and it produces the same result. I hope if someone else knows the correct answer share us here. thanks.

好吧,这不是我使用Laravel ORM的问题的正确答案,但我使用它并产生相同的结果。我希望如果其他人知道正确的答案在这里分享我们。谢谢。

Define this method in Member model:

在Member模型中定义此方法:

return Member::join('relations', function($join) use($limit) {
            $join->on('relations.from_id', '=', 'members.id')
                ->orOn('relations.to_id', '=', 'members.id');
        })
        ->where(function($where){
            $where->where('relations.from_id', '=', $this->id)
                ->orWhere('relations.to_id', '=', $this->id);
        })
        ->where('members.id', '!=', $this->id)
        ->groupBy('members.id')
        ->get();

So I can use:

所以我可以用:

$member = Member::find(1); // for example user#1
$member->friends(); // a list of eloquent objects of members.