I want to make a bulk update to my users
table. I am not updating multiple rows to the same field value – each field value could be different as it will be supplied from user input.
I want to batch my updates and use a single sql connection to send multiple update statements.
Laravel unprepared
works fine but it doesn’t bind parameters:
DB::unprepared("
UPDATE users set name = 'test1' where id = 43;
UPDATE users set name = 'test2' where id = 54;
");
Where test1
& test2
are variables which have been supplied via user input and therefore need to be bound before executing this sql statement.
I don’t want to sanitise these manually. Is there a way I can bind these parameters through a php/laravel function, but also use this type of bulk update?
2
Answers
As explained in Bulk Update Multiple Records with Separate Data by Bertug Korucu
If you want to bulk update with one query, you can use this format
Which with eloquent would look like this
If you have a lot of entries, would be a good idea to chunk by 500 or 1000 (depends on your DB configuration of placeholder limit)
You can use the the following references :
This one is a package. I can use this for updating few rows from millions of rows