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

lucie's avatar

Laravel 5.0 bulk update

I would like to execute multiple update statements rather than executing them individually in order to avoid multiple DB hits.Following is the code

$updateStatement = '';
    foreach ($users as $user) {
        $i++;
        $updateStatement = $updateStatement. "update users set packet_seqno = '".$i."' where id = ".$user->id . ';  ';      
    }
    \DB::statement($updateStatement);

However, it is not working and throwing error

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update

Can you please help me to fix this?

Here is the dump of the $updateStatement.

"update ppt_import_mortgage1 set packet_seqno = '1' where id = 37;  update ppt_import_mortgage1 set packet_seqno = '2' where id = 39;  update ppt_import_mortgage1 set packet_seqno = '3' where id = 40;  update ppt_import_mortgage1 set packet_seqno = '4' where id = 42;  update ppt_import_mortgage1 set packet_seqno = '5' where id = 43;  update ppt_import_mortgage1 set packet_seqno = '6' where id = 44;  update ppt_import_mortgage1 set packet_seqno = '7' where id = 45;  update ppt_import_mortgage1 set packet_seqno = '8' where id = 46;  update ppt_import_mortgage1 set packet_seqno = '9' where id = 47;  update ppt_import_mortgage1 set packet_seqno = '10' where id = 48;  "
0 likes
0 replies

Please or to participate in this conversation.