Tuesday, February 8, 2022

Laravel: get enum values of a column in MySQL table



    public function get_enum_values($table, $field){
        $enum = [];
        $test=DB::select(DB::raw("show columns from {$table} where field = '{$field}'"));
        if(!empty($test)){
            preg_match('/^enum\((.*)\)$/', $test[0]->Type, $matches);
            foreach( explode(',', $matches[1]) as $value )
            {
                $enum[] = trim( $value, "'" );
            }
        }
        return $enum;
    }

No comments:

Post a Comment