Skip to content

Instantly share code, notes, and snippets.

@ifirmawan
Created April 2, 2018 05:04
Show Gist options
  • Save ifirmawan/a95f720976ab17717a3274522c247af2 to your computer and use it in GitHub Desktop.
Save ifirmawan/a95f720976ab17717a3274522c247af2 to your computer and use it in GitHub Desktop.
Laravel class to get enum values
<?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