Last active
December 11, 2015 02:49
-
-
Save eoconnell/4533687 to your computer and use it in GitHub Desktop.
SQL concatenation helper for Laravel 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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