How to get database field type in Laravel?

后端 未结 9 2088
离开以前
离开以前 2020-12-14 01:38

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

相关标签:
9条回答
  • 2020-12-14 02:13

    In Laravel 5+ (including 6 and 7), you can get the db table column metadata (ie type, default value etc) in the following way:

    use Illuminate\Support\Facades\Schema;
    

    For all columns:

     $columns = Schema::getConnection()->getDoctrineSchemaManager()->listTableColumns('table_name');
     $column = Schema::getConnection()->getDoctrineColumn('table_name'', 'column_name'); //For a single column:
    

    getDoctrineSchemaManager method returns a array of \Doctrine\DBAL\Schema\Column Class Instances.

    getDoctrineColumn method returns the instance of \Doctrine\DBAL\Schema\Column class.

    Couple of methods from \Doctrine\DBAL\Schema\Column class:

    $column->getName();
    $column->getNotnull(); // returns true/false
    $column->getDefault(); 
    $column->getType(); 
    $column->getLength();
    
    0 讨论(0)
  • 2020-12-14 02:14

    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.

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

    $pdo = DB::getPdo();
    

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

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

    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.

    The solution

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

    Sidenote: Doctrine is, in fact, included for its schema-based functionalities - Laravel doesn't use Doctrine's 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:

    $schema = DB:: getDoctrineSchemaManager();
    

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

    0 讨论(0)
  • 2020-12-14 02:14

    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] => 
            )
     )
    
    0 讨论(0)
  • 2020-12-14 02:18

    I use this with 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');

    0 讨论(0)
  • 2020-12-14 02:20

    For Laravel 4:

    After digging in Laravel, this is what I got.

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

    0 讨论(0)
  • 2020-12-14 02:28

    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')));
    
    0 讨论(0)
提交回复
热议问题