Please help me to convert the following query to Laravel Eloquent. I am struggling to covert sub-query and if statements.
SELECT *
FROM (
SELECT
IF (msg.from_id = '2', msg.from_id, msg.to_id) AS my_mail,
IF (msg.to_id = '2', msg.from_id, msg.to_id) AS their_mail,
IF (msg.to_id = '2', msg.recipient_deleted_at, msg.sender_deleted_at) AS deleted_at,
msg.*
FROM msg
WHERE (msg.from_id = '2' OR msg.to_id = '2')
HAVING deleted_at IS NULL
ORDER BY created_at DESC
) msg
GROUP BY msg.my_mail, msg.their_mail
ORDER BY msg.created_at DESC