如何用Laravel查询并展示所有数据库表及其详细结构?
- 内容介绍
- 相关推荐
本文共计415个文字,预计阅读时间需要2分钟。
遇到一个需求,需要修改数据库中所有包含email字段的表的长度,将其改为128位。在Laravel中,首先获取所有表,然后循环判断表中是否存在email字段,并修改其长度。
phpuse Illuminate\Support\Facades\DB;
// 获取所有表$tables=DB::select('SHOW TABLES');
foreach ($tables as $table) { // 获取表名 $tableName=$table->Tables_in_database;
// 检查表中是否存在email字段 $columns=DB::select(DESCRIBE {$tableName}); foreach ($columns as $column) { if ($column->Field=='email') { // 修改email字段长度 DB::statement(ALTER TABLE {$tableName} MODIFY email VARCHAR(128)); break; } }}
遇到一个需求,需要修改数据库中所有包含email的字段的表,要把里面的长度改为128位。Laravel获取所有的表,然后循环判断表里面有没有email这个字段。
代码如下:
use Illuminate\Support\Facades\Schema; 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); }
以上这篇Laravel获取所有的数据库表及结构的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。
本文共计415个文字,预计阅读时间需要2分钟。
遇到一个需求,需要修改数据库中所有包含email字段的表的长度,将其改为128位。在Laravel中,首先获取所有表,然后循环判断表中是否存在email字段,并修改其长度。
phpuse Illuminate\Support\Facades\DB;
// 获取所有表$tables=DB::select('SHOW TABLES');
foreach ($tables as $table) { // 获取表名 $tableName=$table->Tables_in_database;
// 检查表中是否存在email字段 $columns=DB::select(DESCRIBE {$tableName}); foreach ($columns as $column) { if ($column->Field=='email') { // 修改email字段长度 DB::statement(ALTER TABLE {$tableName} MODIFY email VARCHAR(128)); break; } }}
遇到一个需求,需要修改数据库中所有包含email的字段的表,要把里面的长度改为128位。Laravel获取所有的表,然后循环判断表里面有没有email这个字段。
代码如下:
use Illuminate\Support\Facades\Schema; 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); }
以上这篇Laravel获取所有的数据库表及结构的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持易盾网络。

