Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get column type from schema in Laravel 8

I'm getting some columns from various tables within my project and would look to obtain some additional information other than the column name, I'd like to get the column type as well.

I'm usign the Schema::getColumnListing() method and am passing a table into it, I'm able to get the fields, but not the types?

My code returns a list of tables, and columns along with an isChecked state, for purposes at a later stage.

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);
}

I did see that there's a getType() method if I use getDoctrineColumn

like image 447
Ryan H Avatar asked Sep 11 '25 16:09

Ryan H


1 Answers

You can use getColumnType from \Illuminate\Support\Facades\Schema class which takes table name and column name as input

As per your code it would look like

array_push($colsWithFields, [
  'field' => $column,
  'type' => Schema::getColumnType($table,$column),
  'isChecked' => false
]);
like image 89
M Khalid Junaid Avatar answered Sep 14 '25 09:09

M Khalid Junaid