Database configuration
Before running migrations, you need to configure your database connection in the.env file.
Supported databases
The Laravel Blog API supports multiple database systems:- MySQL 5.7+ (recommended)
- MariaDB 10.2+
- PostgreSQL 9.6+
- SQLite 3.8.8+
- SQL Server 2017+
MySQL configuration
Update your.env file with your MySQL database credentials:
The database configuration is defined in
config/database.php, which reads these environment variables.Creating the database
Before running migrations, create the database:Laravel uses
utf8mb4 character set by default, which supports full Unicode including emojis and special characters.Running migrations
Migrations are version control for your database schema. They allow you to modify the database structure in a consistent, repeatable way.Execute all migrations
Run all pending migrations:- Create a
migrationstable to track which migrations have run - Execute all migrations in the
database/migrations/directory - Record each migration in the
migrationstable
Check migration status
View which migrations have run and which are pending:Rollback migrations
Rollback the last batch of migrations:Database schema
The Laravel Blog API includes the following database tables:Users table
Stores user account information for authentication. Migration file:2014_10_12_000000_create_users_table.php
| Column | Type | Description |
|---|---|---|
id | increments | Primary key (auto-incrementing) |
name | string | User’s full name |
email | string | Unique email address |
email_verified_at | timestamp | Email verification timestamp (nullable) |
password | string | Hashed password |
remember_token | string | Token for “remember me” functionality |
created_at | timestamp | Record creation timestamp |
updated_at | timestamp | Record last update timestamp |
Password resets table
Stores temporary tokens for password reset requests. Migration file:2014_10_12_100000_create_password_resets_table.php
| Column | Type | Description |
|---|---|---|
email | string | User’s email address (indexed) |
token | string | Password reset token |
created_at | timestamp | Token creation timestamp |
Password reset tokens expire after a configured time period. This table has no primary key as tokens are temporary and cleaned up periodically.
Creating custom migrations
You can create additional migrations to extend the database schema.Generate a new migration
Migration structure
Each migration class has two methods:- The
up()method runs when executing the migration - The
down()method runs when rolling back the migration - Always implement
down()to allow rollbacks
Common column types
Adding indexes
Database seeding
Seeders populate your database with test or default data.Running seeders
The main seeder class is located atdatabase/seeds/DatabaseSeeder.php:
Creating a seeder
Migrate and seed together
Refresh the database and run all seeders:Production considerations
Running migrations in production
--force flag is required in production environments to bypass the confirmation prompt.
Database backups
Always backup your database before running migrations in production:Troubleshooting
”SQLSTATE[HY000] [1045] Access denied”
Your database credentials in.env are incorrect. Verify:
- Database username
- Database password
- Database host and port
”SQLSTATE[HY000] [2002] Connection refused”
The database server is not running. Start your MySQL service:“Base table or view already exists”
The table already exists in your database. Either:- Drop the table manually:
DROP TABLE table_name; - Rollback migrations:
php artisan migrate:rollback
”Syntax error or access violation: 1071 Specified key was too long”
This occurs on older MySQL versions. Add this toapp/Providers/AppServiceProvider.php:
Next steps
API reference
Explore the available API endpoints
Deployment
Deploy your API to production