I am new to Laravel I have a complex query which is a combination of sub-queries and joins. But not sure how can I convert raw query to Laravel query format. Here is how my query looks like.
I am facing problem mostly dealing with AS as this is not a function available in query builder I am trying to sort out the CASE as well, but u1 and u2 looks undefined to it.
select `ep`.`id`, `ep`.`title`, `ep`.`slug`, `ep`.`status`, `ep`.`active`,`ep`.`visitor_id`,`ep`.`type`, `friends`.`customername`, `ep`.`created_at`, epblock.reference_id from enr_posts as ep
JOIN (SELECT
(CASE WHEN u2.id = 3
THEN u1.name
ELSE u2.name
END ) as customername,
(CASE WHEN u2.id = 3
THEN u1.id
ELSE u2.id
END ) as customer_id,
fr.status
FROM enr_friend_requests fr
JOIN enr_customers u1 ON fr.sent_by=u1.id
JOIN enr_customers u2 ON fr.received_by=u2.id
where (fr.sent_by=3 or fr.received_by=3) AND fr.status="accepted") as friends
ON ep.visitor_id=friends.customer_id
LEFT JOIN enr_post_customer_block as epblock
ON `ep`.`id` = epblock.reference_id AND epblock.customer_id=3
WHERE
`ep`.`active`=1 AND
`ep`.`status`=1 AND
epblock.reference_id IS NULL
ORDER BY
`ep`.`id` DESC
Is there any generator tool online as well? Because few tools I have looked into but they are not working for me.
Thank you!