I have tried to covert the following raw query into Laravel query using Eloquent model and query builder
SELECT *
FROM ((SELECT 'Meetings Offered' AS Source,
'Meetings Chosen' AS Destination,
COUNT(1) AS value,
DATE (created_at) AS day
FROM "typeform_contacts"
WHERE client_signed = 1
GROUP BY DATE (created_at))
UNION
(SELECT 'Meetings Chosen' AS Source,
'4A OR 4B' AS Destination,
COUNT(1) AS Value,
DATE (created_at) AS day
FROM "lawyer_opportunity_meetings"
WHERE contact_type = 'typeform'
GROUP BY DATE (created_at))
UNION
(SELECT '4A OR 4B' AS Source,
'4B Get L times' AS Destination,
COUNT(1) AS Value,
DATE (date_added) AS day
FROM "dripfeed_targets"
WHERE "feed_id" IN (360,361)
GROUP BY DATE (date_added))
UNION
(SELECT '4B Get L times' AS Source,
'4C - Meeting Set' AS Destination,
COUNT(1) AS Value,
DATE (date_added) AS day
FROM "dripfeed_targets"
WHERE "feed_id" IN (361)
GROUP BY DATE (date_added))
UNION
(SELECT '4C - Meeting Set' AS Source,
'4D Follow up' AS Destination,
COUNT(1) AS Value,
DATE (date_added) AS day
FROM "dripfeed_targets"
WHERE "feed_id" IN (362)
GROUP BY DATE (date_added))
UNION
(SELECT '4D Follow up' AS Source,
'' AS Destination,
COUNT(1) AS Value,
DATE (date_added) AS day
FROM "dripfeed_targets"
WHERE "feed_id" IN (363)
GROUP BY DATE (date_added))) AS "TEMP"
The laravel query which I have tried is:
public function meetingSankey() {
$returnArray = array();
$result = DB::query()->fromSub(
typeformContact::selectRaw("'Meetings Offered' AS Source, 'Meetings Chosen' AS Destination, COUNT(1) AS value, DATE(created_at) AS day")
->whereRaw("client_signed = 1")
->groupByRaw('DATE(created_at)')
->union(LawyerOpportunityMeeting::selectRaw("'Meetings Chosen' AS Source, '4A OR 4B' AS Destination, COUNT(1) AS Value, DATE(created_at) AS day")
->whereRaw("contact_type = 'typeform'")
->groupByRaw('DATE(created_at)'))
->union(dripfeedTarget::selectRaw("'4A OR 4B' AS Source, '4B Get L times' AS Destination, COUNT(1) AS Value, DATE(date_added) AS day")
->whereIn('feed_id', [360, 361])
->groupByRaw('DATE(date_added)'))
->union(dripfeedTarget::selectRaw("'4B Get L times' AS Source, '4C - Meeting Set' AS Destination, COUNT(1) AS Value, DATE(date_added) AS day")
->whereIn('feed_id', [361])
->groupByRaw('DATE(date_added)'))
->union(dripfeedTarget::selectRaw("'4C - Meeting Set' AS Source, '4D Follow up' AS Destination, COUNT(1) AS Value, DATE(date_added) AS day")
->whereIn('feed_id', [362])
->groupByRaw('DATE(date_added)'))
->union(dripfeedTarget::selectRaw("'4D Follow up' AS Source, '' AS Destination, COUNT(1) AS Value, DATE(date_added) AS day")
->whereIn('feed_id', [363])
->groupByRaw('DATE(date_added)')), 'TEMP')
->whereBetween('DATE(day)', [Carbon::create(2023,1,1,0)->toDateString(), Carbon::now()->toDateString()])->get();
$returnArray['data'] = $result;
$returnArray['status'] = "success";
return response()->json($returnArray, 200);
}
When I am executing the query it shows the following error; although the column client_signed exist in the table typeform_contacts
Illuminate\Database\QueryException: SQLSTATE[42703]: Undefined column: 7 ERROR: column "client_signed" does not exist
LINE 1: ...created_at) AS day from "typeform_contacts" where client_sig...
If I run the query which is built ifrom the query builder:
(select * from ((select 'Meetings Offered' AS Source, 'Meetings Chosen' AS Destination, COUNT(1) AS value, DATE(created_at) AS day from "typeform_contacts" where client_signed = 1 group by DATE(created_at)) union (select 'Meetings Chosen' AS Source, '4A OR 4B' AS Destination, COUNT(1) AS Value, DATE(created_at) AS day from "lawyer_opportunity_meetings" where contact_type = 'typeform' group by DATE(created_at)) union (select '4A OR 4B' AS Source, '4B Get L times' AS Destination, COUNT(1) AS Value, DATE(date_added) AS day from "dripfeed_targets" where "feed_id" in (360, 361) group by DATE(date_added)) union (select '4B Get L times' AS Source, '4C - Meeting Set' AS Destination, COUNT(1) AS Value, DATE(date_added) AS day from "dripfeed_targets" where "feed_id" in (361) group by DATE(date_added)) union (select '4C - Meeting Set' AS Source, '4D Follow up' AS Destination, COUNT(1) AS Value, DATE(date_added) AS day from "dripfeed_targets" where "feed_id" in (362) group by DATE(date_added)) union (select '4D Follow up' AS Source, '' AS Destination, COUNT(1) AS Value, DATE(date_added) AS day from "dripfeed_targets" where "feed_id" in (363) group by DATE(date_added))) as "TEMP" where "DATE(day)" between 2023-01-01 and 2023-02-01)
It shows the following error:
ERROR: column "DATE(day)" does not exist
LINE 1: ...(363) group by DATE(date_added))) as "TEMP" where "DATE(day)...
It considers DATE(day) to be a column but day is an alias name. Kindly suggest where am I going wrong.