【问题标题】:Get column type from schema in Laravel 8从 Laravel 8 中的模式获取列类型
【发布时间】:2021-05-28 19:01:50
【问题描述】:

我正在从我的项目中的各种表中获取一些列,并希望获得除列名之外的一些其他信息,我也想获取列类型。

我正在使用Schema::getColumnListing() 方法并将表格传递给它,我能够获取字段,但不能获取类型?

我的代码返回一个表和列的列表以及isChecked 状态,以供稍后使用。

foreach ($tables as $table => $data) {
  $cols = Schema::getColumnListing($table);
  $colsWithFields = [];

  foreach ($cols as $key => $column) {
    array_push($colsWithFields, [
      'field' => $column,
      'type' => '', // somehow get type??
      'isChecked' => false
    ]);
  }

  $tables[$table]['isChecked'] = false;
  array_push($tables[$table]['columns'], $colsWithFields);
}

如果我使用getDoctrineColumn,我确实看到有一个getType() 方法

【问题讨论】:

    标签: php laravel schema


    【解决方案1】:

    您可以使用\Illuminate\Support\Facades\Schema 类中的getColumnType,该类将表名和列名作为输入

    根据您的代码,它看起来像

    array_push($colsWithFields, [
      'field' => $column,
      'type' => Schema::getColumnType($table,$column),
      'isChecked' => false
    ]);
    

    【讨论】:

      猜你喜欢
      • 2022-10-06
      • 1970-01-01
      • 2016-09-17
      • 2021-09-10
      • 2019-02-05
      • 2013-07-09
      • 2023-01-21
      • 2021-02-06
      • 2021-09-12
      相关资源
      最近更新 更多