我需要列出数据库中的表,我找到了这个查询
SHOW TABLES LIKE 'merTrans%'
我想获取表格,但如何使用foreach
在Laravel 5.1中获取表格名称?
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
}
public function hiddenTables()
{
return [
'migrations',
'password_resets',
];
}
public function dbTables()
{
foreach (\Illuminate\Support\Facades\DB::select('SHOW TABLES') as $tables) {
foreach ($tables as $table => $value)
$db[] = $value;
}
return $db;
}
public static function diffTables()
{
return array_diff((new self)->dbTables(), (new self)->hiddenTables());
}
$tables_list = DB::select('SHOW TABLES');
foreach($tables_listas $value)
{
echo $value->Tables_in_YourDatabaseName;
}
echo $value->Tables_in_test;
。作为替代方案,您可以使用information_schema.tables来获取表名列表。
function getTableNames($db_name, $search = ''){
$rows = DB::select("SELECT table_name FROM information_schema.tables WHERE table_schema = '{$db_name}' AND table_name like '%{$search}%'");
$table_names = [];
foreach($rows as $row)
{
$table_names[] = $row->table_name;
}
return $table_names;
}
print_r(getTableName('my_db', 'merTrans'));
由于您现在无法将'reset'
传递到array_map
中,这是我的解决方案:
$tables = array_map(fn($item) => $item->{array_keys(get_object_vars($item))[0]}, DB::select('SHOW TABLES'))
对于那些喜欢使用查询构建器的人:
DB::table('sqlite_master')
->whereIn('type', [ 'table', 'view' ])
->where('name', 'NOT LIKE', 'sqlite_%')
->orderBy('1')
->pluck('name')
->all()
public function debate_list(){
$records = DB::table('table_name')->get();
return view('page_link')->with('records',$records);
}
@foreach($records as $class)
<tr>
<td>{{$class->id}}</td>
<td>
{{$class->name}}
</td>
<td>
{{$class->image_url}}
</td>
<td>
{{ $class->created_at }}
</td>
<td>
<a class="btn btn-primary btn-sm " href="{{route('image_status_list')}}"> VIEW</a>
</td>
</tr>
@endforeach