Jun 19, 2016
6
Level 2
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)'
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;")
3 likes
Please or to participate in this conversation.