title: Extending Laravel 4 connection class?
body: <p>Is there any way to extend the Laravel connection class?</p>
  
  <p>Basically I need the power of the <code>insert()</code> command, but I need a few more words added to the query. Laravel doesn't have an <code>ON DUPLICATE KEY</code> function. So I need to be able to pass my array of data into the function, and have it take care of the rest. </p>
  
  <p>I tried a raw query, but I don't really know what to do to make my multidimentional array of data work with the SQL, Laravel seems to do it like magic.</p>
  
  <p>I want to have something like:</p>
  
  <pre><code>DB::table('table')-&gt;insert($data);
  </code></pre>
  
  <p>And in the Laravel insert function, which is this:</p>
  
  <pre><code>public function insert($tableName, array $data, array $types = array())
  {
      $this-&gt;connect();
  
      if (empty($data)) {
          return $this-&gt;executeUpdate('INSERT INTO ' . $tableName . ' ()' . ' VALUES ()');
      }
  
      return $this-&gt;executeUpdate(
          'INSERT INTO ' . $tableName . ' (' . implode(', ', array_keys($data)) . ')' .
          ' VALUES (' . implode(', ', array_fill(0, count($data), '?')) . ')',
          array_values($data),
          is_int(key($types)) ? $types : $this-&gt;extractTypeValues($data, $types)
      );
  }
  </code></pre>
  
  <p>Have this instead:</p>
  
  <pre><code>public function insertIgnoreDuplicates($tableName, array $data, array $types = array(), $col)
  {
      $this-&gt;connect();
  
      if (empty($data)) {
          return $this-&gt;executeUpdate('INSERT INTO ' . $tableName . ' ()' . ' VALUES ()');
      }
  
      return $this-&gt;executeUpdate(
          'INSERT INTO ' . $tableName . ' (' . implode(', ', array_keys($data)) . ')' .
          ' VALUES (' . implode(', ', array_fill(0, count($data), '?')) . ') ON DUPLICATE KEY SET $col=$col',
          array_values($data),
          is_int(key($types)) ? $types : $this-&gt;extractTypeValues($data, $types)
      );
  }
  </code></pre>
  
  <p>I saw a couple of blogs on extending the class, but they say the only way to do so is to reimplement each function, which is bad when it comes to Laravel upgrades.</p>
  
  <p>Unless someone has a different option to do a:</p>
  
  <pre><code>DB::table('table')-&gt;insert($data);
  </code></pre>
  
  <p>And add the duplicate key constraint, I would love to hear.</p>
  
  <p>I've tried this:</p>
  
  <pre><code>$columns = implode(", ", array_keys($data));
  $escaped_values = array_map('mysql_real_escape_string', array_values($data));
  $values  = implode("', '", $escaped_values);
  if (DB::statement("INSERT INTO `my_table` ($columns) VALUES ($values) ON DUPLICATE KEY UPDATE col_a = col_a")) {
      return true;
  } else {
      return false;
  }
  </code></pre>
  
  <p>Which I found on another SO post, but I keep getting "mysql_real_escape_string() expects parameter 1 to be string, array given"</p>
  
  <p>So any help is appreciated. I really wish Laravel had this default functionality because all of the behind the scenes work really makes things easier to accomplish.</p>
  
  <p>Thanks for all the help.</p>