Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

trendsideas's avatar

How do I list all tables in Artisan Tinker?

I am using a sqlite database in a Laravel project and I want to list all the tables. How would I do this?

I tried using $tables = DBselect('SHOW TABLES'); but that throws this error: Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1 near "SHOW": syntax error (SQL: SHOW TABLES)'

0 likes
6 replies
trendsideas's avatar

@tomo_pongrac Thanks but it returns this error:

Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1 near "show": syntax error (SQL: show tables)'
trendsideas's avatar

@GwynBleidd Thanks but it returns a similar error,

Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error: 1 near ".": syntax error (SQL: .tables)'

GwynBleidd's avatar
Level 2

@trendsideas Uh, had to familiarize myself with sqlite a little for this :-)

Apparently, sqlite stores table names and schema in a special table called SQLITE_MASTER, so:

DB::select('select * from sqlite_master where type="table"')

and you'll get results like this:

     {#724
       +"type": "table",
       +"name": "migrations",
       +"tbl_name": "migrations",
       +"rootpage": "2",
       +"sql": "CREATE TABLE "migrations" ("migration" varchar not null, "batch" integer not null)",
     },

Update: or rather like this if you want it clearer

DB::select("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;")

Source: https://www.sqlite.org/faq.html#q7

3 likes

Please or to participate in this conversation.