Have you looked at eloquents updateOrCreate.
https://laravel-news.com/firstornew-firstorcreate-firstor-updateorcreate
And query builder has https://laravel.com/docs/8.x/queries#update-or-insert
But if you use raw, bind the parameters.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
How (if possible) can I write the following query using Laravel's query builder?
INSERT INTO Summary (dy, foo, ct, blah_total)
ON DUPLICATE KEY UPDATE
ct = ct + VALUE(ct),
blah_total = blah_total + VALUE(bt)
SELECT DATE(dt) as dy, foo,
COUNT(*) as ct, SUM(blah) as bt)
FROM Staging
GROUP BY 1, 2;
So insert values from one table into another table, but aggregated, and if values exist update them.
I've tried DB::table('...')->insertUsing() but that does not seem to do it and now I'm kinda unsure if it's even possible to do in query builder? Or do I have to go the hard route of writing this RAW and take care of sql injection etc myself?
Please or to participate in this conversation.