Created
April 2, 2018 05:04
-
-
Save ifirmawan/a95f720976ab17717a3274522c247af2 to your computer and use it in GitHub Desktop.
Laravel class to get enum values
This file contains 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 | |
namespace App; | |
use Illuminate\Support\Facades\DB; | |
class CostumSql { | |
protected $_table; | |
public function __construct($table_name){ | |
$this->_table = $table_name; | |
} | |
public function getEnumValues($field=''){ | |
$type = DB::select( DB::raw('SHOW COLUMNS FROM '.$this->_table.' WHERE Field = "'.$field.'"') )[0]->Type; | |
preg_match('/^enum\((.*)\)$/', $type, $matches); | |
$enum = array(); | |
foreach (explode(',', $matches[1]) as $value) { | |
$v = trim( $value,"'"); | |
$enum[] = $v; | |
} | |
return $enum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment