SQLSTATE[42S02] Base table or view not found
This MySQL error means your query references a table that does not exist in the connected database. In Laravel it almost always means migrations have not run, or you are connected to the wrong database.
- About the error
- Why do I see this error
- Solution
- Run your migrations
- Confirm you're on the right database
- Check the model's table name
About the error
In Laravel the message reads:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'my_app.users' does not exist
The query is valid SQL, but the table it names isn't present in the database you're connected to.
Why do I see this error
- Your migrations haven't run, so the table was never created.
- You're connected to the wrong database (wrong
DB_DATABASE, or a fresh/empty database). - The table name has a typo, or you renamed it without updating the model's
$table. - A migration failed halfway and the table was rolled back or never committed.
Solution
Run your migrations
The most common fix, especially on a fresh checkout or a new environment:
php artisan migrate
To check what has and hasn't run:
php artisan migrate:status
On a development database you can rebuild everything from scratch:
php artisan migrate:fresh
Be careful: migrate:fresh drops every table first. Never run it against a database with data you care about.
Confirm you're on the right database
Verify your connection settings point where you think they do:
DB_DATABASE=my_app
Then check the table really exists there:
SHOW TABLES FROM my_app;
If the value changed recently, clear any cached config so Laravel reads the new one:
php artisan config:clear
Check the model's table name
If Eloquent guesses the wrong table name, set it explicitly:
class Customer extends Model
{
protected $table = 'customers';
}
See environment variables in Laravel for how the connection is configured.