Laravel - Bulk insert on duplicate key update large data set
<p>I have approx 80k records that I need to run an insert/update script on several times per day.</p>

<pre><code>INSERT INTO `my_rankings` (`id`, `rank`) VALUES (1,100),(2,99)(3,102)...(80000,3) 
  ON DUPLICATE KEY UPDATE `rank` = values(`rank`);
</code></pre>

<p>These records are currently in an array format:</p>

<pre><code>$rankings = [
  ['id' =&gt; 1, 'rank' =&gt; 100],
  ['id' =&gt; 2, 'rank' =&gt; 99],
  ['id' =&gt; 3, 'rank' =&gt; 102],
  ...
  ['id' =&gt; 80000, 'rank' =&gt; 3],
]
</code></pre>

<p>Is there a nice / performant way I can run the above mentioned update query?</p>

<p>I have looked at Eloquent's <code>Model::updateOrCreate(...)</code>. However I don't think I can use this for bulk insert/updates.</p>

<p>I want to avoid using <code>foreach</code> on my <code>$rankings</code> array and insert/updating individual records, because the script will take far too long.</p>

<p>I have an answer for this question below <a href="https://stackoverflow.com/a/34815725/1239122">https://stackoverflow.com/a/34815725/1239122</a>, however it is far from elegant.</p>