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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18562684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:48:20  来源:igfitidea点击:

How to get database field type in Laravel?

phpdatabaselaravel

提问by Hao Luo

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 integerif I specify table userand column age?

integer如果指定 tableuser和 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 的迁移一样,它需要与数据库无关。

回答by Hao Luo

For Laravel 4:

对于 Laravel 4:

After digging in Laravel, this is what I got.

在 Laravel 中挖掘后,这就是我得到的。

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

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

回答by S?? On???

For Laravel 5.2 - 7.x

对于 Laravel 5.2 - 7.x

use Illuminate\Database\Schema\Builder::getColumnType()

Illuminate\Database\Schema\Builder::getColumnType()

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

e.g.

例如

DB::getSchemaBuilder()->getColumnType('user', 'age')

You may need to run

你可能需要运行

composer require doctrine/dbal

if you haven't already.

如果你还没有。

回答by fideloper

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.

Eloquent 和 Database 类使用 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 标准的 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

旁注:事实上,Doctrine 包含在其基于模式的功能中 - Laravel 不使用 Doctrine 的 ORM

See hereon 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 实例的同一个连接对象上看到这里,我们可以获得学说连接和模式管理器。您应该能够调用:

$schema = DB:: getDoctrineSchemaManager();

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

然后,您可以使用架构管理器(此处的文档)来获取所需的内容。

回答by Kresimir Pendic

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

回答by Edgars Praulins

Use this expression with a dial-down function to show you array details of all your table's fields:

将此表达式与下拉函数一起使用以显示所有表字段的数组详细信息:

dd(DB::select(DB::raw('SHOW FIELDS FROM tablename')));

回答by Pawan Kumar

GET All Details of fields of table

获取表字段的所有详细信息

DB::select('describe table_name');

Example:-

例子:-

DB::select('describe users');

Response will be like this

响应将是这样的

 Array
(
    [0] => stdClass Object
        (
            [Field] => id
            [Type] => int(11)
            [Null] => NO
            [Key] => PRI
            [Default] => 
            [Extra] => auto_increment
        )

    [1] => stdClass Object
        (
            [Field] => user_group_id
            [Type] => int(11) unsigned
            [Null] => YES
            [Key] => MUL
            [Default] => 
            [Extra] => 
        )

    [2] => stdClass Object
        (
            [Field] => username
            [Type] => varchar(100)
            [Null] => YES
            [Key] => MUL
            [Default] => 
            [Extra] => 
        )
 )

回答by Tjeu Moonen

Within the model: laravel5.x

模型内:laravel5.x

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

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

回答by simhumileco

Generalizing:

概括:

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

It works on Laravel 5.3.

它适用于 Laravel 5.3。