How to fetch the tables list in database in Laravel 5.1

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.


I've been using this:

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

It requires doctrine/dbal as a dependency. But some migration features already need DBAL to work.


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.

EDIT : FOR LIKE CASES

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