Skip to content

Instantly share code, notes, and snippets.

@pedroborges
Created April 26, 2014 20:43
Show Gist options
  • Save pedroborges/11330542 to your computer and use it in GitHub Desktop.
Save pedroborges/11330542 to your computer and use it in GitHub Desktop.
Optimize SQL queries when updating a bunch of records at once. (From: https://laracasts.com/forum/1442-best-way-to-update-400-database-rows)
<?php
$checked = [];
$unchecked = [];
foreach($input as $id => $enabled)
{
if ($enabled) $checked[] = $id;
else $unchecked[] = $id;
}
Component::whereIn('id', $checked)->update(['checked' => true]);
Component::whereIn('id', $unchecked)->update(['checked' => false]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment