gpt4 book ai didi

php - 在 Laravel 中获取列类型

转载 作者:可可西里 更新时间:2023-11-01 07:17:58 35 4
gpt4 key购买 nike

我正在创建一个 Laravel 项目,我需要为其动态检索 (MySQL) 数据库中某些表的列名及其类型。目前,这是我的解决方案:

$columnTypes = array();
$columns = Schema::getColumnListing($tableName);
foreach($columns as $columnName) {
$columnTypes[$columnName] = DB::connection()->getDoctrineColumn($tableName, $columnName)->getType()->getName();
}

不幸的是,这需要大量查询,因此需要大量时间(每个表最多约 100 毫秒)。

有没有更快的方法来检索列的类型?

最佳答案

想想,更快的将使用(对于 MySQL):

$tables = array[/* table list */];
foreach($tables as $table){
$table_info_columns = DB::select( DB::raw('SHOW COLUMNS FROM "'.$table.'"'));

foreach($table_info_columns as $column){
$col_name = $column['Field'];
$col_type = $column['Type'];
var_dump($col_name,$col_type);
}
}

关于php - 在 Laravel 中获取列类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25856513/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com