6

What do I want to achieve?

Get all table names inside a database and echo them using @foreach @endforeach. My database name is demoproject and it has some tables inside it like users, list1, list2, list3 and I need to echo all the tables.

When using var dump I get bool(true) or when echoing I get 1 with this code DB::statement('show tables from demoproject');

Why do I want to do this?

I'm making an admin panel, where I can edit tables and their rows. That's why I need to use @foreach to link them to another page.

I only need to know how I can echo all tables inside that database to continue with this project.

I'm using Laravel 4.

4
  • 1
    Why not just include PHPMyAdmin in your application? Commented Oct 18, 2013 at 8:30
  • Because I need to encrypt some values using php. And I don't want all admins to edit some rows. Plus I'll be adding descriptions on how to edit/insert values etc.. Commented Oct 18, 2013 at 8:33
  • 3
    Could you please stop questioning my method of doing this? Commented Oct 18, 2013 at 8:35
  • 1
    Why not? comments intended for clarification. If you don't feel like answering them - just don't answer. Commented Oct 18, 2013 at 8:43

3 Answers 3

34

A little bit late, however, you can run $tables = DB::select('SHOW TABLES'); to get all tables.

Sign up to request clarification or add additional context in comments.

2 Comments

My only question here is that you wouldn't be able to iterate through this list without already knowing the name of the database I think. It comes back like this: {"Tables_in_notes":"gdocs"},{"Tables_in_notes":"migrations"} So to get a list, you'd have to know the project is called "notes" right?
@TommyNicholas, it's not a project name, but database name which you can usually get by env('DB_DATABASE')
7
select * from information_schema.tables where table_schema='database_name'

you can also do this to test or whatnot:

$pdo = DB::connection()->getPdo();
//run queries the normal pdo way

For PGSQL;

    if (config('database.default') == 'pgsql') {
        $database_name = 'laravel';
        $tables = DB::select("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public'");

        foreach ($tables as $table) {
            $tablename = $table->table_name;
            if ($tablename == 'model_has_permissions' || $tablename == 'password_resets'|| $tablename == 'model_has_roles' 
                || $tablename == 'model_has_permissions'|| $tablename == 'role_has_permissions') {
                continue;
            }
                DB::select("
                SELECT setval(pg_get_serial_sequence('$table->table_name' , 'id'), coalesce(max(id)+1, 1), false)
                FROM ".$tablename.";
            ");
            
        }
    }

Comments

0

I just did this in Laravel 5.2 with Microsoft SQL 2012

$tables = DB::connection('<connection_name')
               ->table('INFORMATION_SCHEMA.TABLES')
               ->select('*')
               ->get();
dd($tables);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.