Created
October 23, 2017 14:05
-
-
Save pthiers/050e96f1b20cd3f17ac540424a2ef2b7 to your computer and use it in GitHub Desktop.
Dynamic column mariadb array to column_create
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
namespace App\Utils; | |
use Illuminate\Support\Collection; | |
class MariaDB | |
{ | |
/** | |
* @param $arr array|Collection | |
* @return string | |
*/ | |
public static function arrayToColumnCreate($arr){ | |
if($arr instanceof Collection) | |
$arr = $arr->toArray(); | |
$return = []; | |
foreach ($arr as $key => $value){ | |
$return[] = "'".trim($key,"'")."'"; | |
if(is_array($value) || $value instanceof Collection){ | |
$return[] = self::arrayToColumnCreate($value); | |
}elseif(is_integer($value)){ | |
$return[] = (int) $value; | |
}else{ | |
$return[] = "'".trim($value,"'")."'"; | |
} | |
} | |
return empty($return) ?'null' : "column_create(".implode(',',$return).")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment