SQL查询到Eloquent转换

时间:2018-02-23 18:55:46

标签: php sql laravel eloquent

如何将以下SQL语句转换为Eloquent?

SELECT * FROM `messages` WHERE (`to`=$myID AND `from`=$guestID) OR (`to`=$guestID AND `from`=$myID)

1 个答案:

答案 0 :(得分:4)

parameter grouping使用whereorWhere个闭包:

Message::where(function($q) use($myId, $guestId) {
    $q->where('to', $myId)->where('from', $guestId);
})
->orWhere(function($q) use($myId, $guestId) {
    $q->where('to', $guestId)->where('from', $myId);
})
->get();