zoukankan      html  css  js  c++  java
  • Laravel获取所有的数据库表及结构

    遇到一个需求,需要修改数据库中所有包含email的字段的表,要把里面的长度改为128位。Laravel获取所有的表,然后循环判断表里面有没有email这个字段。代码如下:

    use IlluminateSupportFacadesSchema;
    use DB;
    
    public function getDatabaseColumns() {
        $tables = DB::select('show tables');
        $tables = array_column($tables, 'Tables_in_new_bcc_web');
        $columns = ['email', 'user_name', 'nick_name', 'first_name', 'last_name'];
        // dd(Schema::getConnection());
        foreach ($tables as $key => $value) {
            foreach ($columns as $k => $v) {
                if (Schema::hasColumn($value, $v)) {
                    $table[] = $value;
                };
            }
            // $columns[] = Schema::getColumnListing('users');
        }
        $table = array_unique($table);
        dd($table);
    }
    Schema::getColumnListing('user');
    Schema::hasColumn($table, $column_name);

    这里记一笔,比知道有没有更好的方法一步获取到当前连接的数据库里面的所有的表,我是用原生的sql语句show tables查出所有表,然后取出Tables_in_new_bcc_web这一列,然后才得到所有的表名,然后再去循环。
    找到一个更棒的方式:

    public function getDatabaseColumns() {
        $tables = array_map('reset', DB::select('SHOW TABLES'));
        $columns = ['email', 'user_name', 'nick_name', 'first_name', 'last_name'];
        foreach ($tables as $key => $value) {
            foreach ($columns as $k => $v) {
                if (Schema::hasColumn($value, $v)) {
                    $table[] = $value;
                };
            }
        }
        $table = array_unique($table);
        dd($table);
    }


    https://blog.csdn.net/zhezhebie/article/details/78589812?locationNum=3&fps=1
  • 相关阅读:
    HDU2502 月之数 组合数
    HDU1128 Self Numbers 筛选
    HDU2161 Primes
    HDU1224 Free DIY Tour 最长上升子序列
    HDU2816 I Love You Too
    winForm窗体设置不能随意拖动大小
    gridview 中SelectedIndexChanged 事件获得该行主键
    关于bin和obj文件夹。debug 和release的区别
    winform最小化时在任务栏里隐藏,且显示在托盘里
    wcf异常处理
  • 原文地址:https://www.cnblogs.com/lxwphp/p/9341983.html
Copyright © 2011-2022 走看看