Get column type from schema in Laravel 8

143
February 27, 2021, at 1:40 PM

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

Answer 1

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
]);
READ ALSO
Javascript Sprite Animation Not Transparent

Javascript Sprite Animation Not Transparent

New to javaScript as a language but a fair amount of programming and developer experience

109
Remove WooCommerce Tags when print invoice

Remove WooCommerce Tags when print invoice

I have a wordpress websiteI have been facing a problem with the website

48
How do I use an API in Discord.js?

How do I use an API in Discord.js?

I was looking for a cool way I could automate some interesting news articles on my discord serverI wanted to use webhooks at first but then decided to go with APIs

136
Android Room - “Cannot figure out how to save this field into database.”

Android Room - “Cannot figure out how to save this field into database.”

I try to create Room database and insert values to it, but I got this error:

144