如何在Laravel 5.1中获取数据库中的表列表

时间:2015-11-02 13:41:54

标签: php mysql laravel-5.1

我需要列出tables中的database,我找到了

的查询
SHOW TABLES LIKE  'merTrans%'

获取表格,但我如何使用foreach获取Laravel 5.1中的表名?

16 个答案:

答案 0 :(得分:38)

要列出数据库中的表格,您可以

$tables = DB::select('SHOW TABLES');
foreach($tables as $table)
{
      echo $table->Tables_in_db_name;
}

您必须将db_name更改为数据库的名称。

编辑:类似案例

foreach ($tables as $table) {
    foreach ($table as $key => $value)
        echo $value;
}

答案 1 :(得分:23)

我一直在使用它:

$tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();

它需要学说/ dbal作为依赖。但是一些迁移功能已经需要DBAL才能工作。

答案 2 :(得分:17)

要获得包含所有数据库的快速数组,您可以使用以下代码:

// Iterate over the results of SHOW TABLES
// strip off all the objects and keys.
$tables = array_map('reset', \DB::select('SHOW TABLES'));

对我而言,这似乎是最优雅的解决方案。

答案 3 :(得分:9)

对于Postgres用户:

$(document).ajaxSuccess( function(event, xhr, options) { if (xhr.url !== 'the url im listening for') return; $('option:contains("United States"):not(:contains("Minor"))').prop("selected", true); }); 不受支持,所以你必须做一些更多的hackey。

SHOW TABLES

确保填写table_catalog(我猜这与数据库相同)。您可能需要稍微调整一下结果。

答案 4 :(得分:5)

在Laravel中你可以使用:

$tables = DB::select('SHOW TABLES');

答案 5 :(得分:4)

$tables = \DB::select("SHOW TABLES LIKE 'merTrans%'");
foreach ($tables as $table) {
  echo head($table);
}

答案 6 :(得分:3)

如果您需要对所有表进行一些迁移更改。

在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');
        }
    );
}

当您需要使用具有以下命名结构的表来执行某些操作(在上述情况下-删除一列)时,这是相关的:some_1_tablesome_2_tablesome_3_table等。 因此,基本上可以用DB::select('SHOW TABLES');代替Schema::getAllTables()

答案 7 :(得分:2)

因为我没有添加评论的声誉,但是,我将此作为答案发布。

推荐Bharat的LIKE CASES

foreach ($tables as $table) {
    echo array_shift(array_values($table));
}

如果您不喜欢双重foreach,但请确保var_dump $ table,

($tables = DB::select('SHOW TABLES');

准确了解您正在处理的内容。

答案 8 :(得分: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
    }

答案 9 :(得分:1)

在Laravel项目中添加流动的东西: - 在Controller文件中: -

 public function debate_list(){
         $records = DB::table('table_name')->get();
        return view('page_link')->with('records',$records);
    }

在page_link.blade.php中添加流程: -

@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

答案 10 :(得分:1)

我用它来获取所有带有列的表:

$schema = collect(DB::connection()->getDoctrineSchemaManager()->listTableNames())->map(function ($item, $key) {
  return [
    'name' => $item,
    'columns' => DB::getSchemaBuilder()->getColumnListing($item)
  ];
});

答案 11 :(得分:0)

另一个解决方案是,您不需要使用数据库名称。 “当前”仅占据第一列。

function getTables()
{
    $tables = DB::select('SHOW TABLES');

    $tables = array_map('current',$tables);

    return $tables;
}

答案 12 :(得分:0)

从数据库的“ Laravel”

中找到表的 列表
        $tables_list = DB::select('SHOW TABLES');
        
        foreach($tables_listas $value)
        {
              echo $value->Tables_in_YourDatabaseName;
        }

' Tables_in_YourDatabaseName '的意思是:例如,如果您的数据库名称是“ test” 然后

echo $ value-> Tables_in_test;

答案 13 :(得分:0)

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());
    }

答案 14 :(得分:0)

作为替代方案,您可以使用 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'));

答案 15 :(得分:0)

对于那些喜欢使用查询构建器的人:

DB::table('sqlite_master')
    ->whereIn('type', [ 'table', 'view' ])
    ->where('name', 'NOT LIKE', 'sqlite_%')
    ->orderBy('1')
    ->pluck('name')
    ->all()