Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch the tables list in database in Laravel 5.1

I need to list out the tables in database, I found this query

SHOW TABLES LIKE  'merTrans%' 

to get the tables but how could I use the foreach to get the table names in Laravel 5.1?

like image 957
Balachandiran Avatar asked Nov 02 '15 13:11

Balachandiran


People also ask

How do I fetch a table?

The syntax is: SELECT column1, column2 FROM table1, table2 WHERE column2='value'; In the above SQL statement: The SELECT clause specifies one or more columns to be retrieved; to specify multiple columns, use a comma and a space between column names.


2 Answers

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; } 
like image 58
Bharat Geleda Avatar answered Oct 03 '22 06:10

Bharat Geleda


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.

like image 43
carlosvini Avatar answered Oct 03 '22 06:10

carlosvini