我正在列出DB
中所有以user->id + _
为前缀的表(例如:2_my_table
),如下所示:
$account = Auth()->user();
$tables = DB::select("SHOW TABLES LIKE '" . $account->id . "_%'");
这有效并返回如下数组:
array:1 [▼
0 => {#577 ▼
+"Tables_in_mydb (2_%)": "2_country_list"
}
]
为什么将(2_%)
搜索模式添加到Tables_in_mydb
属性中?
稍后在尝试描述这样的表时,这会导致问题:
if (count($tables)) {
foreach ($tables as $table) {
$table->columns = DB::select('describe '.$table->Tables_in_mydb);
$table->rows = DB::select('SELECT COUNT(*) AS count FROM '.$table->Tables_in_mydb);
}
}
答案 0 :(得分:0)
像这样固定。但是仍然不了解原始问题。
$tables = collect(DB::select('show tables'))->filter(function ($val) use (&$account) {
foreach ($val as $key => $tbl) {
$tbl_prefix = $account->id . "_";
$tbl_prefix_length = strlen($tbl_prefix);
if (substr($tbl, 0, $tbl_prefix_length) == $tbl_prefix) {
return $tbl;
}
}
});
答案 1 :(得分:0)
您可能需要做很多事情。
通过使用get_object_vars
,可以将对象值放入数组中。然后使用array_values来获取值:
$table_name_arr = array_values(get_object_vars($table));
这将提供一个数组(0 => '2_country_list')
,您可以使用$table_name_arr[0]
轻松获得它
单线使用
$table_name = array_values(get_object_vars($table))[0];