BillO
BillO

Reputation: 15

SQL query to Eloquent conversion

How do I transform below SQL statement to Eloquent?

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

Upvotes: 0

Views: 47

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

Use where and orWhere closures for parameter grouping:

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();

Upvotes: 4

Related Questions