如何在Laravel中获取数据库字段类型?

时间:2021-10-09 22:46:11

Is there a way to get the datatype of a database table field? Almost like a inverse of migration.

是否有方法获得数据库表字段的数据类型?就像迁移的逆过程。

For example, if the migration of a users table column looks like

例如,如果用户表列的迁移类似

...
$table->integer('age')
...

Is there a function that will return integer if I specify table user and column age?

如果我指定表用户和列年龄,是否有一个函数会返回整数?

I'm not interested in a specific database implementation, (mysql_field_type()). Like Laravel's migration, it needs to be database agnostic.

我对特定的数据库实现不感兴趣(mysql_field_type()))。就像Laravel的迁移一样,它需要与数据库无关。

6 个解决方案

#1


26  

After digging in Laravel, this is what I got.

在拉拉维尔挖掘之后,我得到了这个。

DB::connection()->getDoctrineColumn('users', 'age')->getType()->getName()

DB::连接()- > getDoctrineColumn(“用户”,“年龄”)- >方法()- > getName()

#2


5  

Short answer: Yes, that functionality exists

After scouring the code, I found that you could. Skip below to "The Solution" to see it.

在搜索代码之后,我发现你可以。跳过下面的“解决方案”来查看它。

Eloquent, and the Database classes, use PDO, which does not tie you a specific SQL-based database.

eloqua和数据库类使用PDO,不会将特定的基于sql的数据库捆绑在一起。

Therefore, you should be able to do something like this:

因此,你应该可以这样做:

$pdo = DB::getPdo();

Note that the connection object can return the instance of PDO.

注意,连接对象可以返回PDO的实例。

There are some methods like getColumnMeta, but they aren't fully supported across all drivers.

有一些方法,比如getColumnMeta,但是它们并不是在所有的驱动程序中都被完全支持。

However, some googling seems to point out that the best way might be to use ANSI-standard INFORMATION_SCHEMA - using sql queries to get that information.

然而,一些谷歌人似乎指出,最好的方法可能是使用ANSI-standard INFORMATION_SCHEMA——使用sql查询来获取信息。

The solution

Lastly, Laravel includes the Doctrine library as a dependency, which does contain some schema functionality.

最后,Laravel将Doctrine库作为一个依赖项,它确实包含一些模式功能。

Sidenote: Doctrine is, in fact, included for its schema-based functionalities - Laravel doesn't use Doctrine's ORM

Sidenote: Doctrine实际上是由于基于原理的功能而包含在内的——Laravel没有使用教义的ORM

See here on the same connection object where we retrieved the PDO instance, we can get the doctrine connection and schema manager. You should be able to call:

在我们检索PDO实例的相同连接对象上,我们可以获得doctrine连接和模式管理器。你应该能够打电话:

$schema = DB:: getDoctrineSchemaManager();

You can then use the schema manager (docs here) to get what you're after.

然后,您可以使用模式管理器(这里的文档)来获取您想要的内容。

#3


3  

I use this with laravel 4.1,,

我用这个和laravel 4。1,

$schema = \DB::getDoctrineSchemaManager();
$tables = $schema->listTables();

foreach ($tables as $table) {
    echo "<i>".$table->getName() . " </i><b>columns:</b><br>";
    foreach ($table->getColumns() as $column) {
        echo ' - ' . $column->getName() . " - " . $column->getType()->getName() . "<br>";
    }
}

or to get specific table use this: $columns = $schema->listTableColumns('user');

或者要获得特定的表,可以使用以下方法:$columns = $schema->listTableColumns('user');

#4


2  

For Laravel 5.2 - 5.5+, use Builder::getColumnType()

对于Laravel 5.2 - 5.5+,使用Builder: getColumnType()

DB::getSchemaBuilder()->getColumnType($tableName, $colName)

#5


0  

Generalizing:

概括:

DB::connection()->getDoctrineColumn($tableName, $colName)
    ->getType()
    ->getName();

It works on Laravel 5.3.

它适用于Laravel 5.3。

#6


0  

Within the model: laravel5.x

    $temp = $this->newQuery()->fromQuery("SHOW FIELDS FROM ".$this->getTable());

    foreach($temp as $val){
       echo 'Field: '.$val->Field;
       echo 'Type: '.$val->Type;
   }

#1


26  

After digging in Laravel, this is what I got.

在拉拉维尔挖掘之后,我得到了这个。

DB::connection()->getDoctrineColumn('users', 'age')->getType()->getName()

DB::连接()- > getDoctrineColumn(“用户”,“年龄”)- >方法()- > getName()

#2


5  

Short answer: Yes, that functionality exists

After scouring the code, I found that you could. Skip below to "The Solution" to see it.

在搜索代码之后,我发现你可以。跳过下面的“解决方案”来查看它。

Eloquent, and the Database classes, use PDO, which does not tie you a specific SQL-based database.

eloqua和数据库类使用PDO,不会将特定的基于sql的数据库捆绑在一起。

Therefore, you should be able to do something like this:

因此,你应该可以这样做:

$pdo = DB::getPdo();

Note that the connection object can return the instance of PDO.

注意,连接对象可以返回PDO的实例。

There are some methods like getColumnMeta, but they aren't fully supported across all drivers.

有一些方法,比如getColumnMeta,但是它们并不是在所有的驱动程序中都被完全支持。

However, some googling seems to point out that the best way might be to use ANSI-standard INFORMATION_SCHEMA - using sql queries to get that information.

然而,一些谷歌人似乎指出,最好的方法可能是使用ANSI-standard INFORMATION_SCHEMA——使用sql查询来获取信息。

The solution

Lastly, Laravel includes the Doctrine library as a dependency, which does contain some schema functionality.

最后,Laravel将Doctrine库作为一个依赖项,它确实包含一些模式功能。

Sidenote: Doctrine is, in fact, included for its schema-based functionalities - Laravel doesn't use Doctrine's ORM

Sidenote: Doctrine实际上是由于基于原理的功能而包含在内的——Laravel没有使用教义的ORM

See here on the same connection object where we retrieved the PDO instance, we can get the doctrine connection and schema manager. You should be able to call:

在我们检索PDO实例的相同连接对象上,我们可以获得doctrine连接和模式管理器。你应该能够打电话:

$schema = DB:: getDoctrineSchemaManager();

You can then use the schema manager (docs here) to get what you're after.

然后,您可以使用模式管理器(这里的文档)来获取您想要的内容。

#3


3  

I use this with laravel 4.1,,

我用这个和laravel 4。1,

$schema = \DB::getDoctrineSchemaManager();
$tables = $schema->listTables();

foreach ($tables as $table) {
    echo "<i>".$table->getName() . " </i><b>columns:</b><br>";
    foreach ($table->getColumns() as $column) {
        echo ' - ' . $column->getName() . " - " . $column->getType()->getName() . "<br>";
    }
}

or to get specific table use this: $columns = $schema->listTableColumns('user');

或者要获得特定的表,可以使用以下方法:$columns = $schema->listTableColumns('user');

#4


2  

For Laravel 5.2 - 5.5+, use Builder::getColumnType()

对于Laravel 5.2 - 5.5+,使用Builder: getColumnType()

DB::getSchemaBuilder()->getColumnType($tableName, $colName)

#5


0  

Generalizing:

概括:

DB::connection()->getDoctrineColumn($tableName, $colName)
    ->getType()
    ->getName();

It works on Laravel 5.3.

它适用于Laravel 5.3。

#6


0  

Within the model: laravel5.x

    $temp = $this->newQuery()->fromQuery("SHOW FIELDS FROM ".$this->getTable());

    foreach($temp as $val){
       echo 'Field: '.$val->Field;
       echo 'Type: '.$val->Type;
   }