Skip to content

Instantly share code, notes, and snippets.

@eoconnell
Last active December 11, 2015 02:49
Show Gist options
  • Save eoconnell/4533687 to your computer and use it in GitHub Desktop.
Save eoconnell/4533687 to your computer and use it in GitHub Desktop.
SQL concatenation helper for Laravel 4
<?php
class DBUtil {
/**
* Concatenate fields using the driver's valid SQL operator.
*
* @param array
*
* @example
*
* DBUtil::concat(array("first", "' '", "last"));
*
* => "CONCAT(first, ' ', last)" // MySQL
* => "first + ' ' + last" // SQL Server
* => "first || ' ' || last" // Default
*/
public static function concat($values = array())
{
$env = Config::get('database.default');
$driver = Config::get("database.connections.{$env}.driver");
switch ($driver) {
case 'mysql':
$result = implode(", ", $values);
return "CONCAT({$result})";
case 'sqlsrv':
return implode(" + ", $values);
default:
return implode(" || ", $values);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment