Laravel Bulk UPDATE

eloquent, laravel, laravel-4, laravel-5

Solution

In case someone land in this page like me, laravel allows a bulk update as:

`$affectedRows = Voucher::where('id', '=', $voucher->id)->update(array('slug' => Str::random(32)));`

See "Updating A Retrieved Model" under http://laravel.com/docs/4.2/eloquent#insert-update-delete

Problem

I'm trying to update a table containing a slug value with random slugs for each record. ``` $vouchers = Voucher->get(); // assume 10K for example foreach ($vouchers as $voucher) { $q .= "UPDATE vouchers set slug = '" . Str::random(32) . "' WHERE id = " . $voucher->id . ";"; } DB::statement($q); ``` There are about 2 million records so I need to perform this as a bulk. Doing it as separate records is taking way too long. I can't seem to find a way to bulk run them, say in groups of 10K or something. Tried a bunch of variations of `->update()` and `DB::statement` but can't seem to get it to go.

Original source