Back to Error Center

SQLSTATE table not found

Error Message

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.posts' doesn't exist

Cause

The referenced database table does not exist. Migrations have not been run or the table name in the model/migration is incorrect.

Solution

Run php artisan migrate to create the tables. Check the table name in the migration and ensure it matches. If using a custom table name, set it in the model.

Code Example

Fix
// Run all pending migrations
php artisan migrate

// Check migration status
php artisan migrate:status

// If the model uses a different table name
class Post extends Model
{
    protected $table = 'blog_posts'; // Custom table name
}

Debugging Steps

  1. Run php artisan migrate:status to check pending migrations
  2. Run php artisan migrate to apply pending migrations
  3. Check the table name in the migration file
  4. Verify the model's $table property if customized

Prevention

Always run migrations after pulling new code. Include migration files in version control. Test migrations in development before deploying.