Reputation: 1010
I got this code in my view to get the columns of a specific table
$columns = \Schema::getColumnListing('posts')
So I made a foreach
loop to get all columns' names. But now I'm making a CRUD generator so I'll need the column type to know which type of fields I'll make for it. The code above just gave me the columns' names. How can I get also the column type?
Update:
I searched for the file which contains getColumnListing method and I found a method called getColumnType($table, $column)
. It returns a string.
I tried to use it but i got this error:
FatalErrorException in MySqlConnection.php line 64:
Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found
Upvotes: 2
Views: 2339
Reputation: 883
you can use getColumnType() method in your foreach.
This package is necessary .
composer require doctrine/dbal
Upvotes: 0
Reputation: 1010
I searched for getColumnListing
method in the project and I found it in a file called Builder.php
and in the exact above of getColumnListing
method I found a method called getColumnType($table, $column)
I tried to use getColumnType
method using this line of code:
$type = \Schema::getColumnType('{{collection}}s', $col);
But I got this error: FatalErrorException in MySqlConnection.php line 64:
Class 'Doctrine\DBAL\Driver\PDOMySql\Driver' not found
And this was because a missed package called doctrine/dbal
which is not included anymore with laravel 5.2
So I run this command composer require doctrine/dbal
and I got no errors anymore.
Thanks for @pari who told me to install doctrin/dbal.
Upvotes: 2
Reputation: 1656
Try This Code
$tables = ['tablename'];
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);
}
}
Upvotes: 1