Database 如何在laravel查询生成器上获取主键名称

Database 如何在laravel查询生成器上获取主键名称,database,laravel,query-builder,laravel-query-builder,Database,Laravel,Query Builder,Laravel Query Builder,有没有办法使用Laravel查询生成器获取表的主键名?我能做点像这样的事吗 $key = DB::table($someTable)->getPrimaryKey(); 我查阅了文档和API,但找不到任何参考资料 提前谢谢 该类具有一个函数,该函数是public 在模型类中,您可以使用$this->getKeyName()访问主键。您可以在模型上使用->getKey(): $someModel->getKey() 或者,如果您只是尝试获取列的名称,而没有模型的实例: app(app\Mo

有没有办法使用Laravel查询生成器获取表的主键名?我能做点像这样的事吗

$key = DB::table($someTable)->getPrimaryKey();
我查阅了文档和API,但找不到任何参考资料

提前谢谢

该类具有一个函数,该函数是
public

在模型类中,您可以使用
$this->getKeyName()

访问主键。您可以在模型上使用
->getKey()

$someModel->getKey()

或者,如果您只是尝试获取列的名称,而没有模型的实例:

app(app\Model::class)->getKeyName()

如果在初始化该查询生成器实例时引用了该模型,则可以检索该模型引用,然后使用它来获取keyName。但是,如果您只是直接调用了一个新的查询生成器,那么在不对表键执行select的情况下(可能是在subselect中),就无法查看主键

SHOW KEYS FROM table WHERE Key_name = 'PRIMARY'
我的做法是:

$result = DB::select(DB::raw("SHOW KEYS FROM {$table} WHERE Key_name = 'PRIMARY'"));
$primaryKey = $result[0]->Column_name;  

我使用mysql,您可以直接使用模式管理器获取表的索引数组:

$indexes = DB::connection()->getDoctrineSchemaManager()->listTableIndexes($table);

然后您可以获得和该索引键关联的列数组:

$columns = $indexes[ 'primary' ]->getColumns();
此函数可用于查找您一无所知的表的主键或唯一键:

public function getTablePrimaryOrUniqueKey($table, $key='') {
    //get the array of table indexes
    $indexes = DB::connection()->getDoctrineSchemaManager()->listTableIndexes($table);

    //abort if there are no indexes
    if(!is_array($indexes)) return false;

    //set up the default array of index keys
    $keys = ['primary', 'unique'];

    //if a key was passed and it is valid, use it...but cast as an array
    if($key!='' && in_array($key, $keys)) $keys = (array) $key;

    //loop through the keys array
    foreach ( $keys as $key ) {
        //keep it honest
        if ( array_key_exists( $key, $indexes ) ) {
            //get the columns for this key
            $columns = $indexes[ $key ]->getColumns();

            //if we have an array of columns, we have what we need
            if ( is_array( $columns ) ) {
                return $columns;
            }
        }
    }

    //if you got here, you did not get find what you were looking for
    return false;
}

getKey实际返回主键的值。是否尝试获取列的名称?使用
->getKeyName()
我没有使用雄辩的模型。我想通过查询生成器来完成,这是不可能的。也许你可以试着去做。我不是在用雄辩的模型。我想通过查询生成器来实现。在这种情况下,我认为您需要使用一些SQL。Laravel似乎没有这个现成的问题。这个问题是基于查询生成器的,请注意任何数据库模型!
$columns = $indexes[ 'primary' ]->getColumns();
public function getTablePrimaryOrUniqueKey($table, $key='') {
    //get the array of table indexes
    $indexes = DB::connection()->getDoctrineSchemaManager()->listTableIndexes($table);

    //abort if there are no indexes
    if(!is_array($indexes)) return false;

    //set up the default array of index keys
    $keys = ['primary', 'unique'];

    //if a key was passed and it is valid, use it...but cast as an array
    if($key!='' && in_array($key, $keys)) $keys = (array) $key;

    //loop through the keys array
    foreach ( $keys as $key ) {
        //keep it honest
        if ( array_key_exists( $key, $indexes ) ) {
            //get the columns for this key
            $columns = $indexes[ $key ]->getColumns();

            //if we have an array of columns, we have what we need
            if ( is_array( $columns ) ) {
                return $columns;
            }
        }
    }

    //if you got here, you did not get find what you were looking for
    return false;
}