php 如何在 Laravel 5.1 中获取数据库中的表列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33478988/
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
How to fetch the tables list in database in Laravel 5.1
提问by Balachandiran
I need to list out the tables
in database
, I found the query for
我需要列出tables
in database
,我找到了查询
SHOW TABLES LIKE 'merTrans%'
get the tables but how could I use the foreach
to get the table names in Laravel 5.1
?
获取表格但我如何使用foreach
来获取表格名称Laravel 5.1
?
回答by Bharat Geleda
To list out the tables in database you can do
要列出数据库中的表,您可以执行
$tables = DB::select('SHOW TABLES');
foreach($tables as $table)
{
echo $table->Tables_in_db_name;
}
You'll have to change the db_name to the name of your database.
您必须将 db_name 更改为数据库的名称。
EDIT : FOR LIKE CASES
编辑:对于类似的情况
foreach ($tables as $table) {
foreach ($table as $key => $value)
echo $value;
}
回答by carlosvini
I've been using this:
我一直在用这个:
$tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();
$tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();
It requires doctrine/dbal as a dependency. But some migration features already need DBAL to work.
它需要学说/dbal 作为依赖项。但是一些迁移功能已经需要 DBAL 才能工作。
回答by Paul Olthof
To get a quick array containing all databases you can use the following piece of code:
要获得包含所有数据库的快速数组,您可以使用以下代码:
// Iterate over the results of SHOW TABLES
// strip off all the objects and keys.
$tables = array_map('reset', \DB::select('SHOW TABLES'));
To me this seems to be the most elegant solution.
对我来说,这似乎是最优雅的解决方案。
回答by Brendan
For Postgres Users:
对于 Postgres 用户:
SHOW TABLES
is not supported so you have to do something a bit more hackey.
SHOW TABLES
不支持所以你必须做一些更hackey的事情。
$tables = DB::select("SELECT table_schema,table_name, table_catalog FROM information_schema.tables WHERE table_catalog = 'YOUR TABLE CATALOG HERE' AND table_type = 'BASE TABLE' AND table_schema = 'public' ORDER BY table_name;")
Make sure you fill out table_catalog (which I guess is equatable to database). You may have to tweak your results a bit.
确保填写 table_catalog(我猜它等同于数据库)。您可能需要稍微调整一下结果。
回答by Mindau
In Laravel you can use:
在 Laravel 中,您可以使用:
$tables = DB::select('SHOW TABLES');
回答by rseyferth
$tables = \DB::select("SHOW TABLES LIKE 'merTrans%'");
foreach ($tables as $table) {
echo head($table);
}
回答by php_bob
Because I don't have the reputation to add a comment, yet, I'm posting this as an answer.
因为我没有添加评论的声誉,所以我将其发布为答案。
A recommendation for Bharat's LIKE CASES
对 Bharat 的 LIKE CASES 的建议
foreach ($tables as $table) {
echo array_shift(array_values($table));
}
if you don't like having a double foreach, but make sure you var_dump $table,
如果你不喜欢双 foreach,但请确保你使用 var_dump $table,
($tables = DB::select('SHOW TABLES');
to see exactly what you are dealing with.
确切地了解您正在处理的内容。
回答by Cristhian Carre?o
protected function getNamesTablesDB(){
$database = Config::get('database.connections.mysql.database');
$tables = DB::select('SHOW TABLES');
$combine = "Tables_in_".$database;
$collection = new \Illuminate\Database\Eloquent\Collection;
foreach($tables as $table){
$collection->put($table->$combine, $table->$combine);
}
return $collection; //or compact('collection'); //for combo select
}
回答by Prafulla Kumar Sahu
Another solution will be, you do not need to use DB name. 'current' will take first column only.
另一种解决方案是,您不需要使用数据库名称。'current' 将只取第一列。
function getTables()
{
$tables = DB::select('SHOW TABLES');
$tables = array_map('current',$tables);
return $tables;
}
回答by Slava Abakumov
In case you need to apply some migration changes to all tables.
如果您需要对所有表应用一些迁移更改。
In Laravel 6 Schema::getAllTables()
became a public method, so
you can do something like this:
在 Laravel 6 中Schema::getAllTables()
成为了一个公共方法,所以你可以这样做:
$tables = array_filter(
Schema::getAllTables(),
static function ($table) {
return preg_match('/some_(\d+)_table/', $table->{'Tables_in_' . env('DB_DATABASE')});
}
);
foreach ($tables as $table ) {
Schema::table(
$table->{'Tables_in_' . env('DB_DATABASE')},
static function (Blueprint $table) {
$table->removeColumn('request_id');
}
);
}
That's relevant when you need to do (in my case above - remove a column) something with tables that have this naming structure: some_1_table
, some_2_table
, some_3_table
, etc.
So basically instead of DB::select('SHOW TABLES');
you can now use Schema::getAllTables()
.
这是相关的,当你需要做的(在我的情况之上-删除列)用的东西有此命名结构表:some_1_table
,some_2_table
,some_3_table
等所以基本上,而不是DB::select('SHOW TABLES');
你现在可以使用Schema::getAllTables()
。