laravel-eloquent-case icon indicating copy to clipboard operation
laravel-eloquent-case copied to clipboard

Using a nested select in a CASE statement causing issues

Open kingthrillgore opened this issue 1 year ago • 0 comments

I have a one-off case where some additional CASE output has to be added to the SELECT, and the criteria for at least one of them relies on a nested select that operates off a count of adjacent records.

The problem at hand is, when defined in the below code, the SQL is malformed and won't work. I'm certain I can make it work solely as Raw, but that kind of defeats the purpose of using this plugin.

The actual SELECT I wish to add is as follows:

$pendingShifts = $client->shifts()->with('caregiver', 'activities')
            ->doesntHave('invoices')
            ->whereNotNull('checked_out_time')
            ->case(function (CaseBuilder $case) {
                $case->when('checked_out_time', '>=', 'INTERVAL 25 HOUR')->then('Requires Attention')
                ->whenRaw('(select count(*) from shift_activities where shift_id = shifts.id) <= 0')->thenRaw('Requires Attention')
                ->else('Complete');
            }, 'VisitStatus')
            ->get();

But the generated SQL appears to be malformed, lacking quotes around the thenRaw statements.

kingthrillgore avatar Jul 02 '24 13:07 kingthrillgore