title: How to insert multiple rows with multiple different columns in Laravel
body: <p>I just tried the following command and received undesired results.</p>
  <pre><code>DB::table('locations')-&gt;insert([
      ['code' =&gt; 'YC', 'name' =&gt; 'York Clifton'],
      ['code' =&gt; 'YK', 'name' =&gt; 'York'],
  
      ['postcode' =&gt; 'DR1',  'name' =&gt; 'Jason'],
      ['postcode' =&gt; 'DLR',  'name' =&gt; 'Beckton']
  ]);
  </code></pre>
  <p>The above would insert data in the table as such:</p>
  <pre class="lang-none prettyprint-override"><code>                Expected                                         Actual
  +-----+--------------+------+----------+        +----+--------------+------+----------+
  | id  |     name     | code | postcode |        | id |     name     | code | postcode |
  +-----+--------------+------+----------+        +----+--------------+------+----------+
  |   1 | York Clifton | YC   | NULL     |        |  1 | York Clifton | YC   | NULL     |
  |   2 | York         | YK   | NULL     |        |  2 | York         | YK   | NULL     |
  |   3 | Jason        | NULL | DR1      |        |  3 | DR1          | Jaso | NULL     |
  |   4 | Beckton      | NULL | DLR      |        |  4 | DLR          | Beck | NULL     |
  +-----+--------------+------+----------+        +----+--------------+------+----------+
  </code></pre>
  <p>The locations table is built using the following snippet:</p>
  <pre><code>$table-&gt;string('name', 100);
  $table-&gt;string('code', 4)-&gt;nullable();
  $table-&gt;string('postcode', 10)-&gt;nullable();
  </code></pre>
  <p>Naturally, my desired outcome is to insert four rows into the database; the first two will fill the <code>code</code> and <code>name</code> fields, whereas the second two inserts will fill <code>postcode</code> and <code>name</code>.</p>
  <p>I have looked at the documentation which says:</p>
  <blockquote>
  <p>The query builder also provides an insert method for inserting records into the database table. The insert method accepts an <strong>array of column names and values</strong>:</p>
  <p>You may even <strong>insert several records into the table with a single call</strong> to insert by passing an array of arrays. Each array represents a row to be inserted into the table.</p>
  </blockquote>
  <p>I'm not entirely sure what Laravel is doing under the hood, but it seems that it prebuilds the insert statements and then inserts the data, ignoring the column name keys.<br />
  <em>To avoid the problem I just separated the insert statements with different column names.</em><br />
  This leads me to think, why bother having column keys for all records if they are superfluous (with the exception of the keys in the first array)? Why not have two parameters for the insert method; one with an array of column names, and one with the data.</p>
  <hr />
  <p>The documentation doesn't say that array keys must all be the same, so if I am missing something then I'd be grateful if someone could provide some insight into why this doesn't work.</p>
  <h3>tl;dr</h3>
  <p>How do I insert multiple rows into a table, when using different column names?</p>