Table Doesn't Exist
The SQL query references a table that doesn't exist in the database.
Explanation
Table doesn't exist is a database error indicating that a SQL query references a table that hasn't been created in the current database. This commonly happens after database migrations haven't been run, when the wrong database is selected, when table names are misspelled, or after a table was dropped. In Laravel, this surfaces as a QueryException wrapping the underlying database error. The error typically includes the exact table name that was referenced.
Common Causes
- Migrations not run
- Wrong database selected
- Table name misspelled
- Table was dropped
- Table prefix mismatch
Solution
Run database migrations: php artisan migrate. Verify the correct database is selected in your .env configuration (DB_DATABASE). Check table name spelling in your queries and models. Use php artisan migrate:fresh to recreate all tables from scratch during development. Ensure the database user has access to the correct database. Check for table prefix configuration in config/database.php. For raw queries, verify the table exists: SHOW TABLES in MySQL or \dt in PostgreSQL.
Code Example
// This fails if the users table doesn't exist
$users = DB::table('users')->get();
// Fix: run migrations first
php artisan migrate
// Check if table exists
Schema::hasTable('users') // returns true/false
// Create table if not exists
if (!Schema::hasTable('users')) {
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
// Check database name in .env
DB_DATABASE=myapp // make sure this matches!
// Raw query to list tables
// MySQL: SHOW TABLES;
// PostgreSQL: \dt
// SQLite: .tables
Error Information
Language
php
Framework
laravel
Difficulty
Beginner
Views
15
Related Errors
PHP
Cannot Redeclare Function
A function with the same name has already been defined in the current scope.
Database
Unknown Column
The SQL query references a column that doesn't exist in the specified table.
Database
TypeORM/SQLAlchemy EntityNotFoundError
The ORM cannot find the requested entity or model in the database.
Database
Access Denied for User
The database rejected the connection because the username or password is incorrect.