I want to get a min value from union table.
As a MySQL Query like below.
SELECT MIN(PromisedDate)
FROM (SELECT PromisedDate FROM JobWorkOrder WHERE `PromisedDate` IS NOT NULL
UNION
SELECT ScheduledDate AS PromisedDate FROM JobPhase WHERE `ScheduledDate` IS NOT NULL) ScheduledTable;
This Query's result is min value of PromisedDate and ScheduledDate in JobWorkOrder and JobPhase Table.
So I built a Laravel query like below.
$JobPhase = JobPhase::where('ScheduledDate', '<>', null)
->select('ScheduledDate AS PromisedDate');
$minDate = JobWorkOrder::where('PromisedDate', '<>', null)
->select('PromisedDate')
->union($JobPhase)
->min('PromisedDate');
But I got a wrong result. The result is
(SELECT MIN(`PromisedDate`) FROM `JobWorkOrder` WHERE `PromisedDate` IS NOT NULL)
union
(SELECT `ScheduledDate` AS `PromisedDate` FROM `JobPhase` WHERE `ScheduledDate` IS NOT NULL)
How can I build a Laravel query?