Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DoppyWoppy's avatar

Laravel Query

I want to build a query in laravel from the given SQL query.

 select 
if(count(*) = 0, 'insert', if(count(*) = 1, 'update', if(count(*) > 1, 'multiple records found', 
    'nothing'))) as required_action 
    from 
      test_importer 
     where 1 
       and record_id = 1881 
       and date_time = "1999-21-19 22:33:18";
0 likes
3 replies
DoppyWoppy's avatar

but is there any way to build this SQL query in laravel?

DoppyWoppy's avatar
DoppyWoppy
OP
Best Answer
Level 1

This one helped me.

$users = DB::select('select if(count(*) = 0, \'insert\', if(count(*) = 1, 
\'update\', if(count(*) > 1, \'multiple\', \'nothing\'))) as required_action from 
test_importer where record_id = ? and date_time = ? ',
[$row['record_id'],$row['date_time']]);

Please or to participate in this conversation.