If you have a good working query I suggest using as is:
See https://laracasts.com/discuss/channels/laravel/sql-native-to-query-builder
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have a query that works and I am trying to get this into Query Builder:
SELECT
tblMap.Site,
CASE
WHEN tblmap.mapelec = 'TRUE' THEN 'Digital Maps'
WHEN tblmap.mappaper = 'TRUE' THEN 'Paper Maps'
ELSE 'Other Maps'
END AS MapType,
tblMap.MapScopeCode, tblMap.MapScopeNum,
CASE
WHEN tblmap.mapplan = 'TRUE' THEN 'Plan View'
END AS MapPlan,
tlkpMapProfTyp.MapProfTypDesc, tlkpMapView.MapView,
tblMap.MapNum, tblMap.MapTitle,
CASE
WHEN tblmap.MapPl = 'TRUE' THEN 'Map includes point-located artifacts/samples'
END AS PL,
CASE
WHEN tblmap.MapCompos = 'TRUE' THEN 'Map is a composite of multiple source maps'
END AS Composite,
tblMap.MapComm
FROM tlkpMapView
RIGHT JOIN (tblMap LEFT JOIN tlkpMapProfTyp ON tblMap.MapProfTyp = tlkpMapProfTyp.MapProfTyp)
ON tlkpMapView.MapViewCode = tblMap.MapViewCode
WHERE tblMap.mapnum <> '514' AND tblMap.Site = '5mt11842'
I think I am good except for the left join that is in the right join. I have tried this:
->rightJoin('tblMap', DB::raw(
"leftJoin tlkpMapProfTyp ON tblMap.MapProfTyp = tlkpMapProfTyp.MapProfTyp)
ON tlkpMapView.MapViewCode = tblMap.MapViewCode"))
but using toSQL() it shows:
right join `tblMap` on leftJoin tlkpMapProfTyp ON tblMap.MapProfTyp
ON tlkpMapView.MapViewCode = tblMap.MapViewCode = ``
It has ‘on’ after tblMap and an extra =‘’ at the end.
I have tried to wrap my head around the leftSubJoin but i am new to this so I haven't gotten my head around it yet.
How can I use QB and get this all to work?
Please or to participate in this conversation.